@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());
}
}``