zoukankan      html  css  js  c++  java
  • spring boot @ConfigurationProperties

    @ConfigurationProperties 是spring-boot中特有的注解。

    使用场景如下: 
    假设application.properties 文件存在redis配置如下:

    redis config

    redis.config.maxTotal=5000 
    redis.config.maxIdle=10 
    redis.config.maxWaitMillis=5000 
    redis.config.testWhileIdle=true 
    redis.config.numTestsPerEvictionRun=100 
    redis.config.timeBetweenEvictionRunsMillis=6000 
    redis.config.testOnBorrow=true 
    redis.config.testOnReturn=true

    redis.config.host=127.0.0.1 
    redis.config.port=6379 
    redis.config.name=localhost_redis 
    redis.config.timeout=5000 
    redis.config.weight=1 
    redis.config.password=

    在定义的Redis配置类中

    @Configuration
    @ConfigurationProperties(prefix = "redis.config")
    @Data
    public class RedisConfiguration {
    
    private int maxTotal;
    private int maxIdle;
    private int maxWaitMillis;
    private boolean testWhileIdle;
    private int numTestsPerEvictionRun;
    private int timeBetweenEvictionRunsMillis;
    private boolean testOnBorrow;
    private boolean testOnReturn;
    
    private String host;
    private String name;
    private int port;
    private int timeout;
    private int weight;
    private String password;
    } 
    

    // @ConfigurationProperties(prefix = “redis.config”) 这个注解,可以使属性文件中的值和类中的属性对应起来。

    注意:在spring boot中除了使用这个注解读取属性文件值外,还可以是用@Value注解。

    @Value(“${spring.redis.weight}”) 
    private int weight;

  • 相关阅读:
    六月计划#2B(6.10-6.16)
    set
    六月计划#2A(6.10-6.16)
    Codevs_1166_[NOIP2007]_矩阵取数游戏_(动态规划+高精度)
    7月17日刷题记录 分治Getting!循环比赛日程表
    倍增ST应用 选择客栈(提高组)
    7月16日做题记录 贪心小练~
    三分查找笔记
    倍增笔记ST表
    分治笔记
  • 原文地址:https://www.cnblogs.com/jtlgb/p/8616805.html
Copyright © 2011-2022 走看看