天天看点

@ConfigurationProperties注解全解析

@ConfigurationProperties注解全解析

@ConfigurationProperties注解类似于@value 可以将application中的配置映射到java变量中 通过@ConfigurationProperties,可以配置是否加载bean

示例:

application.yml

spring: 
  complex:   #重量级模式是否开启 在线上可以不加载开发环境bean
    enable: true      

ConfigProperties 配置是否加载的标识 加载为true ,不加载为fasle 对应上文配置

@ConfigurationProperties(prefix = "spring.complex")
public class ConfigProperties {
 
    private boolean enable;
 
    public boolean isEnable() {
        return enable;
    }
 
    public void setEnable(boolean enable) {
        this.enable = enable;
    }
}      

通过@EnableConfigurationProperties指定上文配置的标识 并通过@ConditionalOnProperty判断 下文实现如果application里面配置的为false或者不配置(默认为false)则swagger相关配置不会加载 提高启动速度

@Configuration(proxyBeanMethods = false)
@EnableSwagger2
@EnableConfigurationProperties(ConfigProperties.class)
@ConditionalOnProperty(prefix = "spring.complex", name = "enable", havingValue = "true", matchIfMissing = false) 指定通过spring.complex.enable属性判断 如果为true 则加载 默认为false
public class SwaggerConfig{
 
 
}