spring boot @EnableWebMvc disable the auto-configuration springMvc principle.

Description:

  If you define your own java profiles, and in spring boot files @EnableWebMvc in the notes, then the default configuration sprig boot will fail. Static files such as the default configuration path: "classpath: / META-INF / resources /", "classpath: / resources /", "classpath: / static /", "classpath: / public /", will fail. And effective configuration will only write their own java configuration.

On principle:

 1. So how did he have to disable the default configuration, tracking down the source code analysis, first of all look at @EnableWebMvc this comment:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class) //导入DelegatingWebMvcConfiguration类
public @interface EnableWebMvc {
}

2. Import DelegatingWebMvcConfiguration class notes, and DelegatingWebMvcConfiguration class inherits WebMvcConfigurationSupport

 

@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
...//省略
}

3. look at springMvc automatic configuration class:

@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class,
        WebMvcConfigurerAdapter.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
public class WebMvcAutoConfiguration {
    .../省略
}    

See the yellow part of the code marking is not, this condition annotations are annotation indicating, if there is no container  WebMvcConfigurationSupport this class,  WebMvcAutoConfiguration configuration class will

Was spring loaded. And we use annotations @EnableWebMvc put WebMvcAutoConfiguration this class is loaded into the spring container. So WebMvcAutoConfiguration default configuration class will fail.

 

Guess you like

Origin www.cnblogs.com/jonrain0625/p/11300569.html