通过@ConditionalOnProperty来控制Configuration是否生效

SpringBoot基础篇Bean之条件注入@Condition使用姿势

[springBoot----@ConditionalOnxxx相关注解总结

@Configuration
@EnableConfigurationProperties(value = { SwaggerProperties.class })//生成是加载配置文件到 Properties bean
@ConditionalOnProperty(name = "swagger.enable",havingValue = "true")//当存在该属性并且值为true时配置bean才生成
@EnableSwagger2
public class SwaggerAutoConfiguration { 
    @Bean
    public Docket createRestApi() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("汽车基础数据维护")
                .description("汽车基础数据维护")
                .termsOfServiceUrl("http://www.asep.com")
                .contact(new Contact("阿飛", "", "afei@ddf.com"))
                .version("1.0.0")
                .build();
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("me.af81.ddf"))
                .paths(PathSelectors.any())
                .build();
    }}

@ConditionalOnProperty属性name,,havingValue

  • application.properties中读取某个属性值,如果该值为空,则返回false;如果值不为空,则将该值与havingValue指定的值进行比较,如果一样则返回true;否则返回false。如果返回值为false,则该configuration不生效;为true则生效。
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容