【@EnableWebMvc的原理】
用途
启用SpringMvc 的 Java 配置类,代替 xml 格式的配置文件。
一、查看运用(注解 @EnableWebMvc
,实现 WebMvcConfigurer
)
@Component("com.ibicd")
@EnableWebMvc
public class AppConfig implements WebMvcConfigurer {
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new MappingJackson2HttpMessageConverter());
}
// 添加拦截器
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new WebContentInterceptor());
}
}
二、@EnableWebMvc
的注解定义
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}
1、查看 DelegatingWebMvcConfiguration
的配置
@Configuration(proxyBeanMethods = false)
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
//具体的代码...
}
2、查看 WebMvcConfigurationSupport
public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {
//具体的代码...
@Bean
@Nullable
public HandlerMapping viewControllerHandlerMapping(
@Qualifier("mvcConversionService") FormattingConversionService conversionService,
@Qualifier("mvcResourceUrlProvider") ResourceUrlProvider resourceUrlProvider) {
ViewControllerRegistry registry = new ViewControllerRegistry(this.applicationContext);
addViewControllers(registry);
AbstractHandlerMapping handlerMapping = registry.buildHandlerMapping();
if (handlerMapping == null) {
return null;
}
PathMatchConfigurer pathConfig = getPathMatchConfigurer();
if (pathConfig.getPatternParser() != null) {
handlerMapping.setPatternParser(pathConfig.getPatternParser());
}
else {
handlerMapping.setUrlPathHelper(pathConfig.getUrlPathHelperOrDefault());
handlerMapping.setPathMatcher(pathConfig.getPathMatcherOrDefault());
}
//视图映射器设置拦截器
handlerMapping.setInterceptors(getInterceptors(conversionService, resourceUrlProvider));
handlerMapping.setCorsConfigurations(getCorsConfigurations());
return handlerMapping;
}
/**
* 提供对共享处理程序拦截器的访问,用于配置HandlerMapping实例。
*
* <p>This method cannot be overridden; use {@link #addInterceptors} instead.
*/
protected final Object[] getInterceptors(
FormattingConversionService mvcConversionService,
ResourceUrlProvider mvcResourceUrlProvider) {
if (this.interceptors == null) {
InterceptorRegistry registry = new InterceptorRegistry();
addInterceptors(registry); // 添加自定义的拦截器
registry.addInterceptor(new ConversionServiceExposingInterceptor(mvcConversionService));
registry.addInterceptor(new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider));
this.interceptors = registry.getInterceptors();
}
return this.interceptors.toArray();
}
/**
* 重写此方法以添加用于控制器调用的Spring MVC拦截器的前置和后置处理。
* @see InterceptorRegistry
*/
protected void addInterceptors(InterceptorRegistry registry) {
}
}
3、查看 addInterceptors
的实现类情况(共2个)
4、查看 DelegatingWebMvcConfiguration
中的方法
@Configuration(proxyBeanMethods = false)
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();
@Override
protected void addInterceptors(InterceptorRegistry registry) {
this.configurers.addInterceptors(registry); // 调用 WebMvcConfigurerComposite 的 addInterceptors 方法
}
}
5、 查看 WebMvcConfigurerComposite
class WebMvcConfigurerComposite implements WebMvcConfigurer {
private final List<WebMvcConfigurer> delegates = new ArrayList<>();
// 查找调用此处的地方
public void addWebMvcConfigurers(List<WebMvcConfigurer> configurers) {
if (!CollectionUtils.isEmpty(configurers)) {
this.delegates.addAll(configurers); // delegates 中添加所有的配置类
}
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
for (WebMvcConfigurer delegate : this.delegates) { // this.delegates 为类中的 delegates
delegate.addInterceptors(registry); //调用每个配置类的 addInterceptors()
}
}
}
6、在哪进行初始化 WebMvcConfigurer
的实现类?
目的就是查找调用 org.springframework.web.servlet.config.annotation.WebMvcConfigurerComposite#addWebMvcConfigurers
的地方:
7、结论
根据上面的图可以知道:
我自己实现的AppConfig 配置类,使用了注解: @Component("com.ibicd")
和 @EnableWebMvc
,而在进行配置类扫描的时候,依赖注入的属性会自动注入,也就是在启动容器时会自动执行: org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration#setConfigurers
,然后再一次调用 addInterceptors()