Spring Boot 2 实践记录之 使用 ConfigurationProperties 注解将配置属性匹配至配置类的属性
在 Spring Boot 2 实践记录之 条件装配 一文中,曾经使用 Condition 类的 ConditionContext 参数获取了配置文件中的配置属性。但那是因为 Spring 提供了将上下文对象传递给 matches 方法的能力。
对于其它的类,想要获取配置属性,可以建立一个配置类,使用 ConfigurationProperties 注解,将配置属性匹配到该类的属性上。(当然,也可以使用不使用 ConfigurationProperties 注解,而使用 @Value注解)
如果要使用 ConfigurationProperties 注解,需要先在 Application 类上添加 @EnableConfigurationProperties 注解:
@SpringBootApplication
@PropertySource(value = {"file:${root}/conf/${env}/application.properties", "file:${root}/conf/${env}/business.properties"})
@EnableConfigurationProperties
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
假设 properties 文件中有如下配置属性:
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.timeout=2000
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
创建 RedisProperties 类如下:
@Component
@ConfigurationProperties(prefix="spring.redis")
@Data
Class RedisProperties {
private String host;
private String port;
private String password;
private String timeout;
private Map<?, ?> pool;
}
引入这个 Bean,就可以读取到相应的属性了。
注意,所有的属性必须有 Setter 和 Getter 方法,上例中,使用了 lombok 的 Data 注解,它会自动为私有属性添加 Setter 和 Getter 方法)。
还要注意属性的类型,如果是获取最后一级的配置属性,类型为 String,如果不是最后一级,则类型为 Map。
import lombok.extern.slf4j.Slf4j; @Slf4j
Class RedisPropertiesTest {
@Autowired
private RedisProperties redisProperties; public void test() {
log.info(redisProperties.getHost());
Map<?, ?> pool = (Map) redisProperties.getPool();
log.info(pool.get("min-idle").toString());
}
}
打印出来的结果是:
127.0.0.1
0
Spring Boot 2 实践记录之 使用 ConfigurationProperties 注解将配置属性匹配至配置类的属性的更多相关文章
- Spring Boot 2 实践记录之 封装依赖及尽可能不创建静态方法以避免在 Service 和 Controller 的单元测试中使用 Powermock
在前面的文章中(Spring Boot 2 实践记录之 Powermock 和 SpringBootTest)提到了使用 Powermock 结合 SpringBootTest.WebMvcTest ...
- Spring Boot 2 实践记录之 MyBatis 集成的启动时警告信息问题
按笔者 Spring Boot 2 实践记录之 MySQL + MyBatis 配置 中的方式,如果想正确运行,需要在 Mapper 类上添加 @Mapper 注解. 但是加入此注解之后,启动时会出现 ...
- Spring Boot 2 实践记录之 Redis 及 Session Redis 配置
先说 Redis 的配置,在一些网上资料中,Spring Boot 的 Redis 除了添加依赖外,还要使用 XML 或 Java 配置文件做些配置,不过经过实践并不需要. 先在 pom 文件中添加 ...
- Spring Boot 2 实践记录之 Powermock 和 SpringBootTest
由于要代码中使用了 Date 类生成实时时间,单元测试中需要 Mock Date 的构造方法,以预设其行为,这就要使用到 PowerMock 在 Spring Boot 的测试套件中,需要添加 @Ru ...
- Spring Boot 2 实践记录之 使用 Powermock、Mockito 对 UUID 进行 mock 单元测试
由于注册时,需要对输入的密码进行加密,使用到了 UUID.sha1.md 等算法.在单元测试时,使用到了 Powermock,记录如下. 先看下加密算法: import org.apache.comm ...
- Spring Boot 2 实践记录之 组合注解原理
Spring 的组合注解功能,网上有很多文章介绍,不过都是介绍其使用方法,鲜有其原理解析. 组合注解并非 Java 的原生能力.就是说,想通过用「注解A」来注解「注解B」,再用「注解B」 来注解 C( ...
- Spring Boot 2 实践记录之 MySQL + MyBatis 配置
如果不需要连接池,那么只需要简单的在pom文件中,添加mysql依赖: <dependency> <groupId>mysql</groupId> <arti ...
- Spring Boot 2 实践记录之 条件装配
实验项目是想要使用多种数据库访问方式,比如 JPA 和 MyBatis. 项目的 Service 层业务逻辑相同,只是具体实现代码不同,自然是一组接口,两组实现类的架构比较合理. 不过这种模式却有一个 ...
- Spring Boot 之日志记录
Spring Boot 之日志记录 Spring Boot 支持集成 Java 世界主流的日志库. 如果对于 Java 日志库不熟悉,可以参考:细说 Java 主流日志工具库 关键词: log4j, ...
随机推荐
- springmvc后台生成验证码
url http://localhost:8080/admin/getCode http://localhost:8080/admin/checkCode controller package com ...
- padding-top和margin-top的区别
学习前端知识,对我们查找页面元素很有帮助,而且自己在原公司时,有参与一个QA系统,自己去设计了这个产品,出了原型图,同时设计了几个页面,希望通过做这个产品提高自己的技术,可是因为离职,所以计划搁浅了, ...
- python之socket运用1
先看下服务端的代码 import socket ip_bind = ("127.0.0.1",3000) sk = socket.socket() sk.bind(ip_bind) ...
- python yaml
一.安装PyYAML http://pyyaml.org/ 二.入门参考 http://www.cnblogs.com/c9com/archive/2013/01/05/2845539.html ht ...
- jquery获取radio单选框的值
1.获取原有单选框的值 var value=$("input[name='is_setting']:checked").val(); 2.获取重选后的单选框的值 <tr> ...
- QFileInfo
https://www.cnblogs.com/findumars/p/10247573.html
- Jmeter从一个Reply Message中获取N个参数的值,然后根据这个参数对后面的操作循环N次(ForEach Controller的用法)
假设Reply Message是这样的: <root><result code="0" msg="success" /><m k= ...
- 获取GUID的GET网址:createguid.com
1.在浏览器的地址栏中输入createguid.com,回车之后即可得到一个GUID 2.在JMeter中可以这样填写HTTP Request 然后通过正则表达式提取器提取GUID <texta ...
- 中介者模式(QQ聊天室我觉得是个很生动的例子简单易懂)
设计模式之中介者模式(Mediator) 一.初识中介者模式 那些年,我们一起上过的大学,班级里有班长,有团书记.想一想如果没有QQ这种通讯工具的话,那么班长或者团支书该怎样下达消息呢??同时,班级上 ...
- 2018.08.17 洛谷P3110 [USACO14DEC]驮运(最短路)
传送门 一道sb最短路,从两个起点和终点跑一边最短路之后直接枚举两人的汇合点求最小值就行了. 代码: #include<bits/stdc++.h> #define N 40005 #de ...