修改配置文件
自定义xxxxxCustomizer
编写配置类: xxxxConfiguration+@Bean替换,增加容器中默认组件
如果是Web应用,则可以创建一个配置类实现WebConfiguration接口,添加@Configuration注解,并在类里创建@Bean来自定义Web功能
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/", "/login", "/css/**", "/fonts/**", "/images/**", "/js/**", "/error/**");
}
}
注意,这里的配置类若加了@EnableWebMvc
,则SpringBoot底层自带的Web自动配置都会失效,即全部都要自己去配置(全面接管WebMvc)
@EnableWebMvc
会@Import(DelegatingWebMvcConfiguration.class)
DelegatingWebMvcConfiguration
类继承自WebMvcConfigurationSupport类
WebMvcConfigurer
并且添加了@Configuration
注解,则它会生效。@EnableWebMvc
默认的自动配置会失效?
在springBoot中WebMvc的自动配置都在WebMvcAutoConfiguration
类中完成的,则这个类中有这么一个注解
而DelegatingWebMvcConfiguration是继承自WebMvcConfigurationSupport
类的
所以添加了@EnableWebMvc
后默认的WebMvcAutoConfiguration
WebMvc自动配置会失效。