惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
I
InfoQ
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Recent Announcements
Recent Announcements
GbyAI
GbyAI
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
美团技术团队
罗磊的独立博客
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
M
MIT News - Artificial intelligence
D
Docker
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs
博客园 - 叶小钗

祈雨的笔记

安全多方计算MPC spark原理解析 kueue执行源码分析 spark on k8s执行源码分析 spark-operator源码解析 系统压测遇到的缓存击穿问题 我的世界PC与安卓联机 蚂蚁金服流量投放平台的AIG改造 G1大对象致Old区占用率高 日志打印导致接口响应率下跌分析 Groovy加载类导致OOM分析 ERROR日志打印导致CPU满载 记OceanBase死锁超时 应用发版期间服务响应超时 Ark Serverless初探 系统优化复盘一二三 The user specified as a definer does not exist Kong网关初探 API网关选型调研 CPU火焰图常用工具 配置中心选型调研 root操作Nginx导致用户组错误 基于Proxifier使用代理 FastJSON字段智能匹配踩坑 Nacos初探 记一次Nginx服务器CPU满荷载故障 基于券系统分库分表的思考 limit不参与SQL成本计算致索引失效 Linux常用性能监控命令 golang低版本http2偶现400
springboot(26)@ConfigurationProperties 与 @Value
祈雨的笔记 · 2018-11-01 · via 祈雨的笔记

转载自简书本文链接地址: Spring Boot @ConfigurationProperties 与 @Value

@ConfigurationProperties@Value都是 Spring 提供的用于从配置文件注入配置信息的方式。很显然,@Value比较适用于配置比较少的场景,而@ConfigurationProperties则更适用于有很多配置的情况。之前写项目的时候从来都没有使用过@ConfigurationProperties几乎每次都是使用@Value。这次遇到了一个比较适合它的场景,在使用的时候还真遇到了一些令人讨厌的小问题,导致开发速度受到了一定的影响。这里记录下来他们之间的使用方式和可能出现的坑,加深一下印象。

注意,我们这里使用application.yml而不是application.properties不过他们基本是可以相互替代的。

Demo for @Value

1
2
3
sso:
clientId: clientId
clientSecret: clientSecret
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Component
@Data
public class ValueConfiguration {
private String clientId;
private String clientSecret;

@Autowired
public ValueConfiguration(
@Value("${sso.clientId}") String clientId,
@Value("${sso.clientSecret}") String clientSecret) {
this.clientId = clientId;
this.clientSecret = clientSecret;
}
}

可以看到@Value是使用非常的简单,只要将注解添加在参数前即可。

Demo for @ConfigurationProperties

在有大量参数的时候,一个个添加@Value就显得麻烦了一点,Spring 提供了另一种方式。

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
@SpringBootApplication
@EnableConfigurationProperties
public class DemoForSpringBootConfigurationApplication {

public static void main(String[] args) {
SpringApplication.run(DemoForSpringBootConfigurationApplication.class, args);
}

@Bean
CommandLineRunner run(ValueConfiguration valueConfiguration) {
return args -> {
System.out.println(valueConfiguration.toString());
};
}

@Bean
CommandLineRunner config(PropertiesConfiguration configuration) {
return args -> {
System.out.println(configuration);
};
}
}

@Component
@ConfigurationProperties(prefix = "oauth")
@Getter
@Setter
@ToString
public class PropertiesConfiguration {
private String clientId;
private String clientSecret;
private String redirectUri;
private String grantType;
}
1
2
3
4
5
6
7
8
9
sso:
clientId: clientId
clientSecret: clientSecret

oauth:
client_id: id // 4
client-secret: secret
redirect_uri: http://aisensiy.github.io
grantType: code
  1. 为了使用@ConfigurationProperties需要在spring boot application上添加EnableConfigurationProperties 的注解,这里遇到的第一个坑
  2. @ConfigurationProperties可以添加前缀,然后其属性就会按照变量的名称默认在 application.* 中寻找指定的变量。这里就是去寻找oauth.clientId这样的配置。如果想要从其他配置文件获取配置内容,可以添加一个额外的注释@PropertySource("classpath:xxx.yml")
  3. 这里的@Setter是来自lombok的注解,它可以自动的帮助添加默认的属性的setter方法。注意,这里的setter方法是必须的,如果没有setter方法,是无法成功获取配置的,这也是我在使用它的时候遇到的又一个坑
  4. @ConfigurationProperties@Value的一个重大区别在于它采用比较灵活的方式寻找配置。可以看到这里的配置可以是驼峰形式,也可以是下划线分割的,还可以是中横线分割的

添加参数验证

@ConfigurationProperties是可以和validation注解一起使用的,这样的好处显而易见:对于一些配置是必须的或者是对格式有要求的,在运行开始的时候就能检测到这些问题可以避免上线之后因为配置不符合有找不到头绪而导致的debug的痛苦过程。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Component
@Getter
@Setter
@ToString
@ConfigurationProperties(prefix = "oauth")
public class PropertiesConfiguration {
@NotBlank
private String clientId;
@NotBlank
private String clientSecret;
@URL
private String redirectUri;
@NotBlank
private String grantType;
}

直接在成员变量上添加注解就可以了,非常的简单。然后可以去尝试添加一些非法的配置试试效果。