WebMvcConfigurationSupport avoid pit Guide

 

  By returning WebMvcConfigurationSupport way to override the automatic default configuration Spring boot, resulting in a static configuration failure can not access resources: But in WebMvcConfigurationadpter (long-obsolete) This is allowed

@Bean
    public WebMvcConfigurationSupport initIndexConfig() {

       return new WebMvcConfigurationSupport() {
           @Override
           protected void addViewControllers(ViewControllerRegistry registry) {
               registry.addViewController("/").setViewName("login");
               registry.addViewController("/index.html").setViewName("login");
           }

           @Override
           protected void addResourceHandlers(ResourceHandlerRegistry registry) {
               registry.addResourceHandler("/**")
                       .addResourceLocations("classpath:/resources")
                       .addResourceLocations("classpath:/static")
                       .addResourceLocations("classpath:/templates")
                       .addResourceLocations("classpath:/public");
           }
           @Override
           protected void addInterceptors(InterceptorRegistry registry) {
               super.addInterceptors(registry);
           }
       };
   }

 WebMvcConfigurerAdapter is an adapter class,

/** @deprecated */
@Deprecated
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
    public WebMvcConfigurerAdapter() {
    }

 The WebMvcConfigurationSupport provide a default is not an empty method implementation

public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {

    private static final boolean romePresent;

    private static final boolean jaxb2Present;

    private static final boolean jackson2Present;

    private static final boolean jackson2XmlPresent;

    private static final boolean jackson2SmilePresent;

    private static final boolean jackson2CborPresent;

 

 So: The return @Bean way to cover a lot of default configuration results achieved can not be used,

Correct use is:

@Configuration
public class AppConfig extends WebMvcConfigurationSupport{

           @Override
           protected void addViewControllers(ViewControllerRegistry registry) {
               registry.addViewController("/").setViewName("login");
               registry.addViewController("/index.html").setViewName("login");
           }

           @Override
           protected void addResourceHandlers(ResourceHandlerRegistry registry) {
               registry.addResourceHandler("/**")
                       .addResourceLocations("classpath:/resources")
                       .addResourceLocations("classpath:/static")
                       .addResourceLocations("classpath:/templates")
                       .addResourceLocations("classpath:/public");
           }
           @Override
           protected void addInterceptors(InterceptorRegistry registry) {
               super.addInterceptors(registry);
           }

You can also choose to directly implement 

WebMvcConfigurer

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/dgwblog/p/11962497.html