Automatic configuration does not take effect after inheriting webmvcconfigurationsupport and how to configure interceptors

There are many articles on the Internet about spring boot2 After 0, it is recommended to directly implement webmvcconfigurer or directly inherit webmvcconfigurationsupport when constructing spring configuration files. It is no problem to implement webmvcconfigurer after testing, but inheriting webmvcconfigurationsupport class will lead to automatic configuration failure.

1、 Inheriting the webmvcconfigurationsupport class is the cause of automatic configuration failure

After the custom configuration class of spring boot inherits webmvcconfigurationsupport, it is found that the automatically configured static resource path (classpath: / meta / resources /, classpath: / resources /, classpath: / static /, classpath: / public /) does not take effect.

First, let's take a look at the definition of automatic configuration class:

This is because there are conditional annotations on the web autoconfiguration class webmvcoautoconfiguration of springboot

@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)

This annotation means that changing the automatic configuration class will take effect only when there is no bean of webmvcconfigurationsupport type in the project class path. Therefore, you need to rewrite the corresponding method after inheriting webmvcconfigurationsupport.

If you want to use automatic configuration to take effect, and you have to rewrite some methods according to your own needs, such as adding viewcontroller, you can use your own configuration class to inherit the webmvcconfigureradapter class. But in spring 5 After version 0, this class was discarded webmvcconfigureradapter. Although it can be used, it doesn't look good.

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

2、 How to configure interceptors by inheriting webmvcconfigurationsupport class

@Configuration
public class MyConfigurer extends WebMvcConfigurationSupport {

@Override
 protected void addInterceptors(InterceptorRegistry registry) {
  registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/emp/toLogin","/emp/login","/js/**","/css/**","/images/**");
  super.addInterceptors(registry);
 }

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

Note this Code:

registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");

Because the automatic configuration will become invalid after inheriting webmvcconfigurationsupport, the default location of static resources should be specified here. At the same time, be careful not to write

registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>