@ConfigurationProperties用法及说明
作者:Huang3stone
@ConfigurationProperties注解用于绑定配置文件中的属性至实体类,只需在实体类上添加此注解,属性名需与配置文件中的名称相同(非驼峰形式),这样即可实现配置文件与实体类属性间的绑定
@ConfigurationProperties用法
@ConfigurationProperties功能类似于@Value
都可以用来获取配置文件中的数据
@ConfigurationProperties 只需要在实体类上添加一个注解,
通过属性名和配置文件的中的名字对照(实体类的属性名和配置文件中的名称要相同,
若配置文件中是card-id形式写法
在实体类的就需要写成驼峰形式,否则会获取不到),进行绑定。
# 配置文件
spring:
redis:
# redis服务器地址
host: 127.0.0.1
# 端口
port: 6379
# 密码
password: 123456
# 默认为0库
database: 2
# 连接超时时间
timeout: 10000ms
lettuce:
pool:
# 最大连接数,默认8
maxActive: 1024
# 最大连接阻塞等待时间,单位毫秒,默认-1ms
maxWait: 10000ms
# 最大空闲连接,默认8
maxIdle: 200
# 最小空闲连接,默认0
minIdle: 5// 通过对比前缀是 spring.redis.lettuce.pool 内容
// 与pojo对象的属性比较并进行绑定
@ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
// 一定此注解,添加到容器中,使用的时候通过自动装配引入即可
@Component
public class Lettuce {
private Integer maxActive;
private String maxWait;
private Integer maxIdle;
private Integer minIdle;
public Integer getMaxActive() {
return maxActive;
}
public void setMaxActive(Integer maxActive) {
this.maxActive = maxActive;
}
public String getMaxWait() {
return maxWait;
}
public void setMaxWait(String maxWait) {
this.maxWait = maxWait;
}
public Integer getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(Integer maxIdle) {
this.maxIdle = maxIdle;
}
public Integer getMinIdle() {
return minIdle;
}
public void setMinIdle(Integer minIdle) {
this.minIdle = minIdle;
}
public Lettuce(Integer maxActive, String maxWait,Integer maxIdle,Integer minIdle){
this.maxActive = maxActive;
this.maxWait = maxWait;
this.maxIdle = maxIdle;
this.minIdle = minIdle;
}
public Lettuce(){
}
@Override
public String toString() {
return "Lettuce{" +
"maxActive=" + maxActive +
", maxWait='" + maxWait + '\'' +
", maxIdle=" + maxIdle +
", minIdle=" + minIdle +
'}';
}
}
@SpringBootTest
class SpringdataDemoApplicationTests {
@Autowired
private Lettuce lettuce;`
@Test
public void test(){
System.out.println(lettuce.toString());
}
}``
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- 深入解析Spring Boot中的@ConfigurationProperties注解
- @ConfigurationProperties及@NestedConfigurationProperty的使用解读
- 将SpringBoot属性配置类@ConfigurationProperties注册为Bean的操作方法
- SpringBoot @ConfigurationProperties + Validation实现启动期校验解决方案
- 解读@ConfigurationProperties和@value的区别
- springboot使用@ConfigurationProperties实现自动绑定配置参数属性
- 解读@ConfigurationProperties的基本用法
