使用@Conditional注入请求范围和原型bean

时间:2020-02-27 15:03:19

标签: spring spring-boot spring-bean

我想在某个类中注入像Foo这样的对象。我要为Foo吃豆子。其中一个是RequestScope,另一个是Prototype。我想为此使用@Conditional。是否有像ConditionalOnHttpRequestExist这样的注释可以分隔这些注入?

1 个答案:

答案 0 :(得分:1)

您可以通过在属性文件中提供任何标志来模拟条件,例如以下示例:

    @EnableSwagger2
    @Configuration
    @ConditionalOnProperty(value = "myapi.enable.swagger", havingValue = "true")
    public class SwaggerConfig {

        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build();
        }

    }

只有在将属性“ myapi.enable.swagger”的值设置为true时,才会配置Swagger。否则不会。

ConditionalOnProperty

相关问题