文章目录
1. properties 文件
对于 properties 文件的读取,采用 @PropertySource 、@Value 注解即可直接获取指定配置项。
1.1. @Value 方式
test-a.properties 文件:
test.a=测试A
test-b.properties 文件(用于测试从不同 properties 文件读取):
test.b=测试B
1.1.1. 配置项读取
@Data
@PropertySource(value = {"classpath:test-a.properties", "classpath:test-b.properties"}, encoding = "UTF-8")
@Component
public class PropertiesDemo {
/**
* test-a.properties 文件中 test.a 属性
*/
@Value("${test.a}")
private String aValue;
/**
* test-b.properties 文件中 test.b 属性
*/
@Value("${test.b}")
private String bValue;
}
@Data : 用于提供 getter,setter。简化代码
@PropertySource : 用于指定 properties 文件,需将 properties 文件放置在 能够获取到路径的地方
@Component: 注入 bean ,不能省略,不然 @Value 注解不能获取到配置项
测试:
注意点:单元测试读取的是 test resources 下的配置文件
1.2. Environment 方式
test-a.properties
test.environment=Environment方式读取配置文件
1.2.1. 配置项读取
@Component
@PropertySource(value = {"classpath:test-a.properties"}, encoding = "UTF-8")
public class EnvironmentDemo {
@Autowired
private Environment environment;
public String getEnvironment() {
return environment.getProperty("test.environment");
}
}
代码解释:
@Component : 注入bean,不可缺少
@PropertySource : 指定配置文件,可指定编码
Environment : 获取配置文件属性的接口
getProperty: 获取配置属性,参数为配置属性名
注意点:同样的需要区分测试资源文件路径
1.3. ConfigurationProperties 方式获取
test-c.properties 文件:
id=1
name=测试C
1.3.1. 读取配置
@Component
@ConfigurationProperties
@PropertySource(value = {"classpath:test-c.properties"}, encoding = "UTF-8")
@Data
public class PropertiesBean {
/**
* ID 对应配置属性:id
*/
private int id;
/**
* name 对应配置属性:name
*/
private String name;
}
@Component: 注入bean,不可少
@ConfigurationProperties: 进行获取配置,对应属性名称(可指定配置前缀等参数)
@PropertySource: 指定文件名,文件编码
@Data : 提供 getter ,setter,简化代码
小结:@Value 的方式获取其实是这种方式的一种泛化方式。@Value 不需要属性名称、字段名称一致,此种方式需要严格一致。
此种方式也适用于 yml 方式获取。
2. yml 文件
前文所说的方法均适用于 yml 文件读取配置属性。
值得注意的是 @PropertySource 不支持配置 yml 文件,我们可以通过 application.yml 配置活跃 yml 文件进行读取。
2.1. 获取示例
application.yml
spring:
profiles:
active: bob
bob 表示活跃的配置文件
application-bob.yml
bob:
id: 2
name: bob姓名
application.yml , application-bob.yml 对应关系图:
2.1.1. 获取配置
@Component
@ConfigurationProperties(prefix = "bob")
@Data
public class YmlDemo {
/**
* 对应 配置文件 bob.id
*/
private int id;
/**
* 对应配置文件 bob.name
*/
private String name;
}
@Component : 注入 bean,同样不能少
@ConfigurationProperties: 获取配置属性,对应上字段名称。此处指定了 prefix(配置属性前缀)
@Data : 提供 getter,setter,简化代码
注意:此种方式获取,会延用项目编码,不需要进行指定编码。故需将 IDE 和项目编码保持一致,避免中文乱码。
3. 静态字段赋值
细心的大佬可能已经发现,之前的方式全是普通的字段,并不是静态字段。
3.1. 背景说明
Spring 不支持采用 @Value 的方式给静态字段赋值。 意味着我们前边的所有方式都不能使用。(直接使用不会报错,但是获取不到属性值)
Spring 提供注入方式包括:setter注入、构造器注入等
3.2. 需求
我们需要项目启动时,便给指定的静态字段进行赋值。
3.3. 解决方案
SpringBoot 没有提供 @Value 的方式读取配置属性给静态变量赋值,但是静态变量支持 setter 注入的方式。即通过 @Value 给 setter 注入参数值,再用 setter 给静态变量赋值。即可实现需求。
properties 文件,延用之前的配置文件
3.3.1. 获取配置
@PropertySource(value = {"classpath:test-a.properties", "classpath:test-b.properties"}, encoding = "UTF-8")
@Component
public class PropertiesStaticDemo {
/**
* test-a.properties 文件中 test.a 属性
*/
public static String aValue;
/**
* test-b.properties 文件中 test.b 属性
*/
public static String bValue;
/**
* 获取配置文件项 test.a 给参数 aValue 赋值,然后通过方法体给 静态属性 PropertiesStaticDemo.aValue 赋值
*/
@Value("${test.a}")
public void setAValue(String aValue) {
PropertiesStaticDemo.aValue = aValue;
}
@Value("${test.b}")
public void setBValue(String bValue) {
PropertiesStaticDemo.bValue = bValue;
}
}
注:
a. 在全部修改为 static 字段时,也需要保留 @Component 注解
b. @PropertySource 指定配置文件、文件编码
3.3.2. 测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class PropertiesStaticTest {
@Test
public void test() {
System.out.println("aValue:" + PropertiesStaticDemo.aValue);
System.out.println("bValue:" + PropertiesStaticDemo.bValue);
}
}
直接通过类名访问。
4. 小结
properties、yml 两个文件的读取原理一样的,只是对应的活跃文件指定方式略有区别。
对于静态属性的赋值,主要是采用 setter 的方式,巧妙的曲线救国。
知识有限,有错有漏欢迎评论指正。谢谢。
5. 完整代码
https://github.com/LamboChen/demo