SpringBoot---初探MVC自动配置原理
2021-05-05 19:30
标签:https factory nts 转换器 cts handler 静态 mvcc res Spring Boot provides auto-configuration for Spring MVC that works well with most applications. The auto-configuration adds the following features on top of Spring’s defaults: If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own If you want to provide custom instances of If you want to take complete control of Spring MVC, you can add your own 进入WebMvcAutoConfiguration,这是webmvc的配置类,有viewResolver的相关配置 ? ContentNegotiatingViewResolver获得最高优先级 进入 可知其实现了 此接口的核心方法是 查看 看看如何获取候选视图的,看看 是从 把所有的视图解析器从BeanFactory找出来,放进 我们自己写一个ViewResolver,注册成bean应该就可以了 调试看看 果然看到了自定义的ViewResolver,并且 可以发现这些方法去 再看一眼 跳转成功 这个类上有一个注解,在做其他自动配置时会导入: 可以看到 看看 只有一个属性configurers,类型是WebMvcConfigurerComposite,Composite是拼合的意思,我暂时将其理解为一些WebMvcConfigurer的集合,再点 也只有一个属性,delegates,类型是 delegate意为委托 SpringBoot:最佳英语学习工具 回到 通过上一步已经知道configurers是 将所有的WebMvcConfigurer相关配置来一起调用!包括我们自己配置的和Spring给我们配置的 If you want to take complete control of Spring MVC, you can add your own 使用 看看 导入了 这是Java方式MVC配置的主类 回到 注意看第四行---> 这是一个条件判断,没有 一旦使用 SpringBoot---初探MVC自动配置原理 标签:https factory nts 转换器 cts handler 静态 mvcc res 原文地址:https://www.cnblogs.com/antonzhao/p/13191738.html5.10 MVC自动配置原理
Spring MVC Auto-configuration
ContentNegotiatingViewResolver
and BeanNameViewResolver
beans.
Converter
, GenericConverter
, and Formatter
beans.
HttpMessageConverters
(covered later in this document).
MessageCodesResolver
(covered later in this document).
index.html
support.
Favicon
support (covered later in this document).
ConfigurableWebBindingInitializer
bean (covered later in this document).
@Configuration
class of type WebMvcConfigurer
but without@EnableWebMvc
.RequestMappingHandlerMapping
, RequestMappingHandlerAdapter
, or ExceptionHandlerExceptionResolver
, and still keep the Spring Boot MVC customizations, you can declare a bean of type WebMvcRegistrations
and use it to provide custom instances of those components.@Configuration
annotated with @EnableWebMvc
, or alternatively add your own @Configuration
-annotated DelegatingWebMvcConfiguration
as described in the Javadoc of @EnableWebMvc
.ViewResolver 视图解析器
@Bean
@ConditionalOnBean(ViewResolver.class)
@ConditionalOnMissingBean(name = "viewResolver", value = ContentNegotiatingViewResolver.class)
public ContentNegotiatingViewResolver viewResolver(BeanFactory beanFactory) {
ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
resolver.setContentNegotiationManager(beanFactory.getBean(ContentNegotiationManager.class));
// ContentNegotiatingViewResolver uses all the other view resolvers to locate
// a view so it should have a high precedence
// ContentNegotiatingViewResolver使用所有其他视图解析器来定位视图,因此它应该具有较高的优先
resolver.setOrder(Ordered.HIGHEST_PRECEDENCE);
return resolver;
}
ContentNegotiatingViewResolver类
查看public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
implements ViewResolver, Ordered, InitializingBean {}
ViewResolver接口
,再看看这个接口吧public interface ViewResolver {
/**
* Resolve the given view by name.
*/
@Nullable
View resolveViewName(String viewName, Locale locale) throws Exception;
}
resolveViewName
,使用我去看看ContentNegotiatingViewResolver类
如何实现这个方法ContentNegotiatingViewResolver类
的resolveViewName方法
getCandidateViews方法
viewResolvers属性
拿到的,看看这个属性如何初始化viewResolvers属性
// 扩展webmvc配置
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
// 将自定义的视图解析器装配
@Bean
public ViewResolver myViewResolver() {
return new MyViewResolver();
}
// 自定义一个视图解析器
public static class MyViewResolver implements ViewResolver {
@Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
return null;
}
}
}
ContentNegotiatingViewResolver
是优先级最高的。FormattingConverter 格式转换器
WebMvcAutoConfiguration类
里,可以找到格式转换的方法。
WebMvcProperties类
拿到了相应的属性值,熟悉的properties类,也就是说这些值是可以直接配置的,prefix为spring.mvc.format
WebMvcProperties类
的结构吧,format
是其中一个静态内部类修改SpringBoot的默认配置
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/ttttt").setViewName("test");
}
扩展原理
WebMvcAutoConfiguration类
是 SpringMVC的自动配置类,里面有一个静态内部类WebMvcAutoConfigurationAdapter
@Import(EnableWebMvcConfiguration.class)
,点进EnableWebMvcConfiguration类
看一下,它继承了DelegatingWebMvcConfiguration类
EnableWebMvcConfiguration类
也是WebMvcAutoConfiguration类
的静态内部类
DelegatingWebMvcConfiguration类
WebMvcConfigurerComposite类
看看List
,和我们刚才的猜测基本一致。
DelegatingWebMvcConfiguration类
,找一个方法参考下@Override
protected void addViewControllers(ViewControllerRegistry registry) {
this.configurers.addViewControllers(registry);
}
WebMvcConfigurerComposite类
,点开这个方法@Override
public void addViewControllers(ViewControllerRegistry registry) {
for (WebMvcConfigurer delegate : this.delegates) {
delegate.addViewControllers(registry);
}
}
全面接管SpringMVC
@Configuration
annotated with @EnableWebMvc
, or alternatively add your own @Configuration
-annotated DelegatingWebMvcConfiguration
as described in the Javadoc of @EnableWebMvc
.@EnableWebMvc
可以让SpringBoot的webmvc配置失效
@EnableWebMvc
注解如何让自动配置失效@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}
DelegatingWebMvcConfiguration类
,这个类在扩展原理第三步出现了,此类继承了WebMvcConfigurationSupport类
/**
* This is the main class providing the configuration behind the MVC Java config.
*/
public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {}
WebMvcAutoConfiguration类
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {}
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
WebMvcConfigurationSupport类
才能让本类生效@EnableWebMvc
,就会使其生效,从而让SpringBoot有关webMVC的自动配置失效