简体   繁体   中英

Spring Conditional bean on @EnableWebMvc

How can I control a bean's creation depending on whether there's not another, different bean in the project annotated with @EnableWebMvc ?

I tried

@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)

as suggested here but no luck. i get this contradicting state when @EnableWebMvc is there

WebMvcAutoConfiguration: Did not match: - @ConditionalOnMissingBean (types: org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; SearchStrategy: all) found beans of type 'org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport' org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration (OnBeanCondition)

and also

ProblemModulesRegisterer matched: - @ConditionalOnMissingBean (types: org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; SearchStrategy: all) did not find any beans (OnBeanCondition)

So it found and not found beans of type WebMvcConfigurationSupport

As another attempt, I also compared the registered beans with and without @EnableWebMvc and noticed that without it, there's a bean called org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration . So I tried a @ConditionalOnMissingBean(WebMvcAutoConfiguration.class) but it still doesn't work. It shows this contradicting state

WebMvcAutoConfiguration matched : - @ConditionalOnClass found required classes 'javax.servlet.Servlet', 'org.springframework.web.servlet.DispatcherServlet', 'org.springframework.web.servlet.config.annotation.WebMvcConfigurer' (OnClassCondition) - found 'session' scope (OnWebApplicationCondition) - @ConditionalOnMissingBean (types: org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; SearchStrategy: all) did not find any beans (OnBeanCondition)

and also

ProblemModulesRegisterer matched: - @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; SearchStrategy: all) did not find any beans (OnBeanCondition)

So WebMvcAutoConfiguration matched (was created) and a dependency of missing WebMvcAutoConfiguration also matched

@ConditionalOnMissingBean will execute your code and what you want is opposite - not execute. Perhaps you should try a mixture of @ConditionalOnClass and @AutoConfigureAfter provided in the example in the link you've posted.

I've used in few database related beans and manipulation of Conditional s and AutoconfigureAfter actually does the trick. Just need a better understanding of your application to be able to provide more concrete suggestion

You need to make sure @ConditionalOnMissingBean annotated bean creation will happen after WebMvcConfigurationSupport creation.

From spring docs ,

Beans on which the current bean depends. Any beans specified are guaranteed to be created by the container before this bean.

Use @DependsOn annotation in addition to @ConditionalOnMissingBean as explained here .

and also make sure you are doing these inside a @configuration annotated class.

@Configuration
public class Config {

    @Bean
    @DependsOn({"webMvcConfigurationSupport"})
    @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
     public FileProcessor fileProcessor(){
        return new FileProcessor();
    }

In this example, fileProcessor will be created only when webMvcConfigurationSupport bean is missing.

The problem was that you can only use @ConditionalOnMissingBean in an autoconfigure context. The javadoc says

The condition can only match the bean definitions that have been processed by the application context so far and, as such, it is strongly recommended to use this condition on auto-configuration classes only

I was trying to use the annotation in a regular application. For more info on creating an autoconfiguration module check the official doc . Once I did it like that (specifically, adding the class to spring.factories ), it worked to detect @EnableWebMvc by using

@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM