今天学习到了SpringBoot 的属性赋值@Value用法
先总结
-
@Value(" 张三 ")
:直接附在属性名上,在Bean初始化时,会赋初始值 -
@Value(" #{ 20 - 2 } ")
:可以用 #{ },里面可以写表达式,当然也可以直接 @Value(" #{ 18 } ") 或 @Value(" 18 ") -
@Value(" ${ person.name } ")
:利用 ${ } 可以取出配置文件中的值
例子
配置类:
1
2
3
4
5
6
7
8
9
|
@PropertySource (value={ "classpath:/person.properties" }) @Configuration public class MainConfigOfPropertyValues { @Bean public Person person() { return new Person(); } } |
-
@Configuration
:告诉 Spring 这是一个配置类 -
@PropertySource
:关联配置文件,使用 @PropertySource 指定读取外部配置文件,保存到运行的环境变量中,然后可以用@Value(" ${ person.name } ") 获取环境变量中的值。
Bean :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
public class Person { /* * 使用@Value赋值: * 1. 基本数值 * 2. 可以写 #{ } * 3. 可以写 ${ },取出配置文件{properties}中的值 */ @Value ( "张三" ) private String name; @Value ( "#{20-2}" ) private int age; @Value ( "${person.nickName}" ) private String nickName; public String getNickName() { return nickName; } public void setNickName(String nickName) { this .nickName = nickName; } public String getName() { return name; } public void setName(String name) { this .name = name; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", nickName=" + nickName + "]" ; } public Person(String name, int age, String nickName) { super (); this .name = name; this .age = age; this .nickName = nickName; } public Person() { super (); // TODO Auto-generated constructor stub } } |
配置文件:
1
|
person.nickName=\u5C0F\u4E09 |
这里面的 \u5C0F\u4E09 表示的是“小三”
而配置文件的位置:
运行:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
public class IOCTest_PropertyValue { //容器创建 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues. class ); //打印容器中Bean的name private void printBeans(AnnotationConfigApplicationContext applicationContext) { String[] definitionName = applicationContext.getBeanDefinitionNames(); for (String name : definitionName) { System.out.println(name); } } @Test public void test01() { printBeans(applicationContext); System.out.println( "-------------------------------------------------------" ); //获得容器中的Person Person person = (Person) applicationContext.getBean( "person" ); System.out.println(person); //获得环境变量中的值 ConfigurableEnvironment environment = applicationContext.getEnvironment(); String property = environment.getProperty( "person.nickName" ); System.out.println( "环境变量:" +property); applicationContext.close(); } } |
运行结果:
@Value的使用及注意事项
为什么使用
使用@Vlue可以实现对于线上项目的一些通用配置的修改;或者修改项目中可能出现变动,但是却又有很多地方都在使用的一些参数,这样我们我可直接通过修改配置文件而不用修改代码的方式来达到参数的修改的目的
参数形式
yml文件
(简单的参数)
1
2
3
|
test1: num: 1 name: xiaoming |
获取数据
1
2
3
4
|
@Value ( "${test1.num}" ) private int num; @Value ( "${test1.name}" ) private String name; |
(数组||集合)
1
2
3
4
5
|
test: array1: aaa,bbb,ccc array2: 111,222,333 array3: 11.1,22.2,33.3 list1: ddd,eeee,111,333,222,444 |
获取数据
1
2
3
4
5
6
7
8
9
|
//数组 @Value ( "${test.array1:}" ) private String[] array1; //集合 @Value ( "#{'${test.list1:}'.split(',')}" ) private List<String> list1; //集合进一步做空数据处理 @Value ( "#{'${test.list:}'.empty ? null : '${test.list:}'.split(',')}" ) private List<String> testList; |
(Map)
1
2
3
|
test: map1: '{"name": "zhangsan", "sex": "male"}' map2: '{"math": "90", "english": "85"}' |
获取数据
1
2
3
4
|
@Value ( "#{${test.map2}}" ) private Map<String,String> map1; @Value ( "#{${test.map2}}" ) private Map<String,Integer> map2; |
前置条件(注意事项)
想要能够很好的使用的使用@Value 需要注意一些前置条件
1. 使用正确的注解
@Value的使用要找对目标,我们所使用的注解的引用时这样的
2. yml文件的格式
如果你使用的是yml文件的话,需要注意yml文件的注意格式问题 ,基本上参数名之后的冒号之后都要加空格,一般情况下,冒号后面加了空格,参数名都会变为蓝色
3. @Value使用的环境要求
- 不能作用于静态变量(static);
- 不能作用于常量(final);
- 不能在非注册的类中使用(类需要被注册在spring上下文中,如用@Service,@RestController,@Component等);
- 使用这个类时,只能通过依赖注入的方式,用new的方式是不会自动注入这些配置的。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_41075649/article/details/83419588