The problem that the automatic configuration does not take effect after spring boot inherits WebMvcConfigurationSupport

  After the custom configuration class of spring boot inherits WebMvcConfigurationSupport, it is found that the automatically configured static resource paths (classpath:/META/resources/, classpath:/resources/, classpath:/static/, classpath:/public/) do not take effect.

  First look at the definition of the auto-configuration class:

  

 

This is because there are conditional annotations on springboot's web auto-configuration class WebMvcAutoConfiguration 

@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)

This annotation means that changing the automatic configuration class will only take effect when the bean of type WebMvcConfigurationSupport is missing  , so after inheriting WebMvcConfigurationSupport, you need to rewrite the corresponding method yourself.

  If you want to use automatic configuration to take effect, and you need to rewrite some methods according to your own needs, such as adding viewController , you can inherit the WebMvcConfigurerAdapter class from your own configuration class. However, after the spring 5.0 version, this class was discarded WebMvcConfigurerAdapter   , although it can still be used, but it does not look good = =.

  

  This is a comment on the class, which means that Java8 will be used after spring 5.0, and in Java8, the interface can have a default method, so this class is unnecessary. So we just need to implement it directly  in the custom configuration class

The WebMvcConfigurer interface is just fine. as follows:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325258961&siteId=291194637