阿里程序猿小密圈解析Spring @Value 属性注入使用(含源代码)

@Value注入

不通过配置文件的注入属性的情况

通过@Value将外部的值动态注入到Bean中,使用的情况有:

注入普通字符串

注入操作系统属性

注入表达式结果

注入其他Bean属性:注入beanInject对象的属性another

注入文件资源

注入URL资源

详细代码见:

@Value("normal")privateString normal;// 注入普通字符串@Value("#{systemProperties['os.name']}")privateString systemPropertiesName;// 注入操作系统属性@Value("#{ T(java.lang.Math).random() * 100.0 }")privatedouble randomNumber;//注入表达式结果@Value("#{beanInject.another}")privateString fromAnotherBean;// 注入其他Bean属性:注入beanInject对象的属性another,类具体定义见下面@Value("classpath:com/hry/spring/configinject/config.txt")privateResource resourceFile;// 注入文件资源@Value("http://www.baidu.com")privateResource testUrl;// 注入URL资源1234567891011121314151617

注入其他Bean属性:注入beanInject对象的属性another

@ComponentpublicclassBeanInject{@Value("其他Bean的属性")privateString another;publicStringgetAnother(){returnanother; }publicvoidsetAnother(String another){this.another = another; }}1234567891011121314

注入文件资源:com/hry/spring/configinject/config.txt

test configuration file1

测试类: 

@SpringBootApplicationpublicclassApplication{publicstaticvoidmain(String[] args) { SpringApplication.run(Application.class, args); }}@RunWith(SpringRunner.class)@SpringBootTest(classes=Application.class)publicclassConfiginjectApplicationTest{@AutowiredprivateBaseValueInject baseValueInject;@TestpublicvoidbaseValueInject(){ System.out.println(baseValueInject.toString()); }}1234567891011121314151617181920

运行测试类

normal=normalsystemPropertiesName=Windows10randomNumber=35.10603794922444fromAnotherBean=其他Bean的属性resourceFile=test configuration filetestUrl=...百度一下,你就知道...略123456

通过配置文件的注入属性的情况

通过@Value将外部配置文件的值动态注入到Bean中。配置文件主要有两类:

application.properties。application.properties在spring boot启动时默认加载此文件

自定义属性文件。自定义属性文件通过@PropertySource加载。@PropertySource可以同时加载多个文件,也可以加载单个文件。如果相同第一个属性文件和第二属性文件存在相同key,则最后一个属性文件里的key启作用。加载文件的路径也可以配置变量,如下文的${anotherfile.configinject},此值定义在第一个属性文件config.properties

第一个属性文件config.properties内容如下: 

${anotherfile.configinject}作为第二个属性文件加载路径的变量值

book.name=bookNameanotherfile.configinject=placeholder12

第二个属性文件config_placeholder.properties内容如下:

book.name.placeholder=bookNamePlaceholder1

下面通过@Value(“${app.name}”)语法将属性文件的值注入bean属性值,详细代码见:

@Component// 引入外部配置文件组:${app.configinject}的值来自config.properties。// 如果相同@PropertySource({"classpath:com/hry/spring/configinject/config.properties", "classpath:com/hry/spring/configinject/config_${anotherfile.configinject}.properties"})public class ConfigurationFileInject{@Value("${app.name}")privateStringappName;// 这里的值来自application.properties,spring boot启动时默认加载此文件@Value("${book.name}")privateStringbookName;// 注入第一个配置外部文件属性@Value("${book.name.placeholder}")privateStringbookNamePlaceholder;// 注入第二个配置外部文件属性@AutowiredprivateEnvironment env;// 注入环境变量对象,存储注入的属性值publicStringtoString(){ StringBuilder sb =newStringBuilder(); sb.append("bookName=").append(bookName).append("\r\n") .append("bookNamePlaceholder=").append(bookNamePlaceholder).append("\r\n") .append("appName=").append(appName).append("\r\n") .append("env=").append(env).append("\r\n")// 从eniroment中获取属性值.append("env=").append(env.getProperty("book.name.placeholder")).append("\r\n");returnsb.toString(); } }1234567891011121314151617181920212223242526272829

测试代码:

Application.java同上文

@RunWith(SpringRunner.class)@SpringBootTest(classes=Application.class)publicclass ConfiginjectApplicationTest { @Autowiredprivate ConfigurationFileInject configurationFileInject; @Testpublic void configurationFileInject(){System.out.println(configurationFileInject.toString()); }}123456789101112

测试运行结果:

bookName=bookNamebookNamePlaceholder=bookNamePlaceholderappName=appNameenv=StandardEnvironment {activeProfiles=[], defaultProfiles=[default], propertySources=[Inlined Test Properties,systemProperties,systemEnvironment,random,applicationConfig: [classpath:/application.properties],class path resource [com/hry/spring/configinject/config_placeholder.properties],class path resource [com/hry/spring/configinject/config.properties]]}env=bookNamePlaceholder

测试属性文件:advance_value_inject.properties

server.name=server1,server2,server3#spelDefault.value=notdefaultHelloWorld_=sss123

测试类AdvanceValueInject:引入advance_value_inject.properties文件,作为属性的注入

@Component@PropertySource({"classpath:com/hry/spring/configinject/advance_value_inject.properties"})public class AdvanceValueInject {...}12345

1.2 #{…}和${…} ###

#{…}用法

通过@Value(“${spelDefault.value}”)可以获取属性文件中对应的值,但是如果属性文件中没有这个属性,则会报错。可以通过赋予默认值解决这个问题,如@Value("${spelDefault.value:127.0.0.1}")

详细代码如下:

// 如果属性文件没有spelDefault.value,则会报错// @Value("${spelDefault.value}")// private String spelDefault2;// 使用default.value设置值,如果不存在则使用默认值@Value("${spelDefault.value:127.0.0.1}")privateString spelDefault;123456789

${…}用法

${…}的{}里面的内容必须符合SpEL表达式,详细的语法,以后可以专门开新的文章介绍,这里只演示简单用法:

// SpEL:调用字符串Hello World的concat方法@Value("#{'Hello World'.concat('!')}")privateString helloWorld;// SpEL: 调用字符串的getBytes方法,然后调用length属性@Value("#{'Hello World'.bytes.length}")privateString helloWorldbytes;1234567

${…}和#{…}混合使用

${...}和#{...}可以混合使用,如下文代码执行顺序:通过${server.name}从属性文件中获取值并进行替换,然后就变成了 执行SpEL表达式#{‘server1,server2,server3’.split(‘,’)}。

// SpEL: 传入一个字符串,根据","切分后插入列表中, #{}和${}配置使用(注意单引号,注意不能反过来${}在外面,#{}在里面) @Value("#{'${server.name}'.split(',')}") privateList servers;123

在上文中在 

需要源代码加群582505643 群公告已经公布代码地址

#{}外面,${}在里面可以执行成功,那么反过来是否可以呢${}在外面,#{}在里面,如代码

// SpEL: 注意不能反过来${}在外面,#{}在里面,这个会执行失败@Value("${#{'HelloWorld'.concat('_')}}") privateList servers2;123

答案是不能。因为spring执行${}是时机要早于#{}。在本例中,Spring会尝试从属性中查找#{‘HelloWorld’.concat(‘_’)},那么肯定找到,由上文已知如果找不到,然后报错。所以${}在外面,#{}在里面是非法操作

小结

#{…} 主要用于加载外部属性文件中的值

${…} 用于执行SpEl表达式,并将内容赋值给属性

#{…} 和${…} 可以混合使用,但是必须#{}外面,${}在里面

需要源代码加群582505643 群公告已经公布代码地址

你可能感兴趣的:(阿里程序猿小密圈解析Spring @Value 属性注入使用(含源代码))