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

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
V
V2EX
C
Check Point Blog
GbyAI
GbyAI
D
Docker
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
T
Troy Hunt's Blog
博客园 - Franky
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Microsoft Security Blog
Microsoft Security Blog
P
Privacy & Cybersecurity Law Blog
WordPress大学
WordPress大学
The Cloudflare Blog
S
SegmentFault 最新的问题
Latest news
Latest news
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
I
InfoQ
博客园 - 【当耐特】
NISL@THU
NISL@THU
A
About on SuperTechFans
T
Tailwind CSS Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Scott Helme
Scott Helme
雷峰网
雷峰网
C
CXSECURITY Database RSS Feed - CXSecurity.com
Security Latest
Security Latest
V
Vulnerabilities – Threatpost
Security Archives - TechRepublic
Security Archives - TechRepublic
A
Arctic Wolf
Hacker News: Ask HN
Hacker News: Ask HN
N
News and Events Feed by Topic
IT之家
IT之家
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
aimingoo的专栏
aimingoo的专栏
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
SecWiki News
SecWiki News
大猫的无限游戏
大猫的无限游戏
S
Security Affairs
The Register - Security
The Register - Security
www.infosecurity-magazine.com
www.infosecurity-magazine.com
L
LINUX DO - 热门话题
T
Tor Project blog

祈雨的笔记

安全多方计算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 hostname in certificate didn't match 常见对称加密原理以及应用 tcp_tw_recycle引起的TCP握手失败 记一次mysql执行DDL导致锁表 mysql磁盘占用查看 mysql对text字段update致磁盘增长 elasticsearch报错index read-only TIME_WAIT与Http的Keep-Alive 记一次TIME_WAIT导致连接数报警 记一次生产事故OOM问题排查 redis分布式锁RedissonLock的实现细节 webservice复杂加密签名(2)java调用 webservice复杂加密签名(1)SoapUI mysql延时关联 利用中间人拦截实现APP内H5窜改 MySQL表字符集不同导致关联查询索引失效 通过SSH隧道远程办公 数据落盘方案 BeanDefinitionRegistryPostProcessor扩展 mysql空间索引 HTTPS攻击 spring循环依赖过程解析 elasticsearch性能优化 mysql IS NULL 使用索引 mysql字符集utf8mb4失效踩坑 常用加密算法 xml与javaBean转换 初探InnoDB MVCC源码实现 mysql索引原理 redis之list源码分析 redis之key过期源码分析 redis之string源码分析 redis之hash源码分析 线程池之ThreadPoolExecutor mysql数据页结构 Using temporary与Using filesort mysql回表致索引失效 springboot(28)HTTP连接池 定时任务之ScheduledThreadPoolExecutor elasticsearch常用script聚合 elasticsearch实现like查询 elasticsearch实现乐观锁 elasticsearch准实时原理 springboot(27)自定义缓存读写机制CachingConfigurerSupport optimizer tracing arthas常用命令 HTTP和HTTPS详解 redis集群选举机制 kafka消息重试 一点压力测试的经验 kafka架构概念 explain分析sql语句字段的解释 JVM问题分析处理手册 logstash过滤器(2)date logstash过滤器(3)dissect logstash编码器(1)json logstash编码器(2)multiline logstash表达式 logstash输入(1)通用选项 logstash输入(3)file logstash过滤器(1)通用选项 logstash输入(2)stdin logstash安装 记一次前端vConsole导致JSON序列化错误排查 解决多个异步操作嵌套问题 fastjson反序列化失败autoType is not support RTMP串流服务 POI自动调整列宽错误 Nginx+Lua实现动态黑名单 使用curl命令模拟POST和GET请求
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;
}

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