本文来源:http://www.mydlq.club/article/61/
. 一、简介
. 1、SpringBoot 中常用读取配置方法
. 2、@Value 和 @ConfigurationProperties 的区别
. 二、使用 @Value 读取配置
. 1、@Value 读取配置参数
. 2、@Value 给参数设定值
. 3、@Value 读取系统属性
. 4、@Value 读取 Bean 的属性
. 5、@Value 使用 SpEL 表达式
. 6、@Value 读取 Resource 资源文件
. 三、使用 @ConfigurationProperties 读取配置
. 1、@ConfigurationProperties 读取配置参数到 String 类型
. 2、@ConfigurationProperties 读取 List 类型参数
. 3、@ConfigurationProperties 读取 Map 类型参数
. 4、@ConfigurationProperties 读取 Time 类型参数
. 5、@ConfigurationProperties 读取 DataSize 类型参数
. 6、@ConfigurationProperties 读取 DataSize 类型参数
. 7、@ConfigurationProperties 读取配置参数并进行 Valid 效验
. 8、从指定配置文件中读取参数
. 四、使用 Environment 对象读取配置
. 五、使用 PropertiesLoaderUtils 读取配置
. 六、读取配置文件示例项目
. 1、Maven 引入相关依赖
. 2、测试的配置文件
. 3、读取配置的多种方法
. 4、配置测试用的 Controller
. 5、启动类
系统环境:
SpringBoot 版本:2.2.2
参考地址:
@ConfigurationProperties 注解使用姿势,这一篇就够了
示例项目 Github 地址:https://github.com/my-dlq/blog-example/tree/master/springboot/springboot-read-config-example
一、简介
在日常开发使用 SpringBoot 框架时,经常有一些配置信息需要放置到配置文件中,我们需要手动读取这些配置到应用中进行一些逻辑,这里整理了一些常用读取配置的方法,简单介绍一下。
1、SpringBoot 中常用读取配置方法
SpringBoot 中常用的读取配置方法有:
使用 @Value 注解读取配置
使用 @ConfigurationProperties 注解读取配置
使用 Environment 对象读取配置
使用 PropertiesLoaderUtils 工具读取配置
2、@Value 和 @ConfigurationProperties 的区别
二者区别 | @ConfigurationProperties | @Value |
---|---|---|
功能 | 批量注入配置文件中的属性 | 一个个指定 |
松散绑定(松散语法) | 支持 | 不支持 |
SpEL | 不支持 | 支持 |
JSR303数据校验 | 支持 | 不支持 |
复杂类型封装 | 支持 | 不支持 |
下面会详细介绍使用各个方法是如何读取配置信息。
二、使用 @Value 读取配置
1、@Value 读取配置参数
application.properties 配置文件内容:
1my.name=mydlq
2my.age=18
使用 @Value 读取配置文件
1@Component
2public class ReadProperties {
3
4 @Value("${my.name}")
5 private String name;
6
7 @Value("${my.age}")
8 private Integer age;
9
10}
并且还可以设置一个默认值,放置未从配置文件中读取到该参数:
1通过 @Value 读取配置文件
2@Component
3public class ReadProperties {
4
5 @Value("${my.name:默认姓名}")
6 private String name;
7
8 @Value("${my.age:18}")
9 private Integer age;
10
11}
2、@Value 给参数设定值
使用 @Value 注解给参数设定值,达到跟“=”号一样的赋值效果:
1@Component
2public class ReadProperties {
3
4 @Value("#{'test value'}")
5 private String value;
6
7}
3、@Value 读取系统属性
使用 @Value 注解读取系统环境参数:
1@Component
2public class ReadProperties {
3
4 @Value("#{systemProperties['os.name']}")
5 private String systemPropertiesName;
6
7}
4、@Value 读取 Bean 的属性
测试用的实体对象:
1@Data
2public class User{
3 private String name;
4 private String age;
5}
使用 @Value 注解读取 Bean 中对象的属性:
1@Component
2public class ReadProperties {
3
4 @Bean
5 public User user(){
6 User user = new User();
7 user.setName("测试");
8 user.setAge("18");
9 return user;
10 }
11
12 @Value("#{user.name}")
13 private String value;
14
15}
5、@Value 使用 SpEL 表达式
在 @Value 注解中可以使用 SpEL 表达式,如下是使用 SpEL 表达式生成随机数:
1@Component
2public class ReadProperties {
3
4 @Value("#{ T(java.lang.Math).random() * 100.0 }")
5 private double random;
6
7}
6、@Value 读取 Resource 资源文件
使用 @Value 可以读取资源文件进行一些操作:
1@Component
2public class ReadProperties {
3
4 @Value("classpath:application.properties")
5 private Resource resourceFile;
6
7 public void test(){
8 // 如果文件存在,就输出文件名称
9 if(resourceFile.exists()){
10 System.out.println(resourceFile.getFilename());
11 }
12 }
13
14}
三、使用 @ConfigurationProperties 读取配置
1、@ConfigurationProperties 读取配置参数到 String 类型
application.properties 配置文件内容:
1my.name=mydlq
使用 @ConfigurationProperties 注解读取对应配置:
1@Configuration
2@ConfigurationProperties(prefix = "my") //配置 prefix 来过滤对应前缀
3public class ConfigurationReadConfig {
4
5 private String name;
6
7 public String getName() {
8 return name;
9 }
10 public void setName(String name) {
11 this.name = name;
12 }
13
14}
注意:使用 @ConfigurationProperties 注解读取配置,则需要配置文件内容中的参数添加统一的前缀,在 @ConfigurationProperties 注解中配置该前缀的值,然后前缀后的属性名要与加 @ConfigurationProperties 注解的类中成员变量名称保持一致。
2、@ConfigurationProperties 读取 List 类型参数
application.properties 配置文件内容:
1my.list[0]=a
2my.list[1]=b
3my.list[2]=c
使用 @ConfigurationProperties 注解读取对应配置:
1@Configuration
2@ConfigurationProperties(prefix = "my")
3p