Use @Conditional for inject request scope and prototype bean

Viewed 582

I want to inject an object like Foo in some class. I have to bean for Foo. One of them is RequestScope and other is Prototype. I want to use @Conditional for this. Is there any annotation like ConditionalOnHttpRequestExist that separate these injection?

1 Answers

You can simulate the condition via providing any flag in properties file, like below sample:

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

    }

Here the Swagger will be configured only if the value of property "myapi.enable.swagger" is set to true otherwise not.

ConditionalOnProperty

Related