value java_Spring @Value 注解 使用总结一

本文详细介绍了Spring框架中@Value注解的使用,包括注入普通字符串、系统变量、表达式结果、其他Bean属性、配置文件属性、文件资源和URL资源等。还探讨了如何通过@PropertySource加载自定义配置文件,并展示了如何处理属性文件中的占位符。此外,文章还提到了@Value注解给静态变量赋值的注意事项和SpEL表达式的应用。
摘要由CSDN通过智能技术生成

Spring @Value 注解 使用总结一

2018年12月13号

发表于:评论 ()

热度:10874

@Value注入

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

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

1、注入普通字符串 如下例

直接附在属性名上,在 Bean 初始化时,会赋初始值

@Value("normal")

private String normal;

2、注入java 系统变量

@Value("#{systemProperties['os.name']}")

private String systemPropertiesName; // 注入操作系统属性

这里的   systemProperties是  java的   System.getProperties()方法获取的 java环境变量 ,查看java系统变量的方法如下

public String getproperties() {

//获取所有的属性

Properties properties = System.getProperties();

//为了看看系统的属性有几个,加了一个计数器

//遍历所有的属性

for (String key : properties.stringPropertyNames()) {

System.out.println(key + "=" + properties.getProperty(key));

}

return null;

}

3、注入表达式结果

#{ } 里面写表 SpEL 达式

@Value("#{ T(java.lang.Math).random() * 100.0 }")

private double randomNumber; //注入表达式结果

4、注入其他Bean属性:注入beanInject对象的属性another,类具体定义见下面(这里不大理解)

@Value("#{beanInject.another}")

private String fromAnotherBean;

5、将配置文件 *.properties 或 *. yml  里 配置的 属性  注入

@Value("${book.name}")

private String bookName;

yml配置如下

book:

name: live

6、注入文件资源

@Value("classpath:com/hry/spring/configinject/config.txt")

private Resource resourceFile; // 注入文件资源

7、注入url资源

@Value("http://www.baidu.com")

private Resource testUrl; // 注入URL资源

@value注入用法 一

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

@Component

public class BeanInject {

@Value("其他Bean的属性")

private String another;

public String getAnother() {

return another;

}

public void setAnother(String another) {

this.another = another;

}

}

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

test configuration file

测试类:

@SpringBootApplication

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

}

@RunWith(SpringRunner.class)

@SpringBootTest(classes=Application.class)

public class ConfiginjectApplicationTest {

@Autowired

private BaseValueInject baseValueInject;

@Test

public void baseValueInject(){

System.out.println(baseValueInject.toString());

}

}

运行测试类

normal=normal

systemPropertiesName=Windows 10

randomNumber=35.10603794922444

fromAnotherBean=其他Bean的属性

resourceFile=test configuration file

testUrl=...

百度一下,你就知道...略

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

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

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

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

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

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

book.name=bookName

anotherfile.configinject=placeholder

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

book.name.placeholder=bookNamePlaceholder

下面通过@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}")

private String appName; // 这里的值来自application.properties,spring boot启动时默认加载此文件

@Value("${book.name}")

private String bookName; // 注入第一个配置外部文件属性

@Value("${book.name.placeholder}")

private String bookNamePlaceholder; // 注入第二个配置外部文件属性

@Autowired

private Environment env; // 注入环境变量对象,存储注入的属性值

public String toString(){

StringBuilder sb = new StringBuilder();

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");

return sb.toString();

}

}

测试代码:

Application.java同上文

@RunWith(SpringRunner.class)

@SpringBootTest(classes=Application.class)

public class ConfiginjectApplicationTest {

@Autowired

private ConfigurationFileInject configurationFileInject;

@Test

public void configurationFileInject(){

System.out.println(configurationFileInject.toString());

}

}

测试运行结果:

bookName=bookName

bookNamePlaceholder=bookNamePlaceholder

appName=appName

env=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

@value 注解 给静态变量赋值

给静态变量赋值    使用

@value("${mail.username}")

private static String mailUsername;

获取不到值必须得用 set方法

给静态变量赋值,可以使用set()方法,其中需要在类上加入@Component注解,方法名(例如setMailUsername)和参数名(例如username)可以任意命名,如下所示:

36534f6ad6da74439cb3f1dce726bdc0.png

调试结果如下:

f8907381f81e2ad24f5523c1f7c33ff3.png

@value注入详细用法 二

测试属性文件:config.properties

server.name=server1,server2,server3

#spelDefault.value=notdefault

HelloWorld_=sss

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

在类里用 @Value 注入 配置文件的属性的时候, 如果配置文件 *.properties 或 *.yml 不是系统默认加载的 , 就必须 在类的开始    用 @Component   @PropertySource   导入配置文件

这里是我理解的 , 还得确认一下

还有@PropertySource的详细的用法还得查查

@PropertySource:关联配置文件,使用 @PropertySource 指定读取外部配置文件,保存到运行的环境变量中,然后可以用     @Value(" ${ person.name } ") 获取环境变量中的值。

@Component

@PropertySource({"classpath:com/hry/spring/configinject/config.properties"})

public class AdvanceValueInject {

...

}

${…}用法

{}里面的内容必须符合 SpEL 表达式, 通过@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}")

private String spelDefault;

#{…}用法

这里只演示简单用法:

// SpEL:调用字符串Hello World的concat方法

@Value("#{'Hello World'.concat('!')}")

private String helloWorld;

// SpEL: 调用字符串的getBytes方法,然后调用length属性

@Value("#{'Hello World'.bytes.length}")

private String helloWorldbytes;

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

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

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

@Value("#{'${server.name}'.split(',')}")

private List servers;

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

// SpEL: 注意不能反过来${}在外面,#{}在里面,这个会执行失败

@Value("${#{'HelloWorld'.concat('_')}}")

private List servers2;

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

小结

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

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

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

(。・v・。)

喜欢这篇文章吗?欢迎分享到你的微博、QQ群,并关注我们的微博,谢谢支持。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值