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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值