博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot源码解析autoconfigure之DispatcherServletAutoConfiguration
阅读量:5740 次
发布时间:2019-06-18

本文共 8793 字,大约阅读时间需要 29 分钟。

hot3.png

说在前面

本次开始spring-boot-autoconfigure源码解析之DispatcherServletAutoConfiguration

 

源码解析

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)@Configuration@ConditionalOnWebApplication@ConditionalOnClass(DispatcherServlet.class)@AutoConfigureAfter(EmbeddedServletContainerAutoConfiguration.class)public class DispatcherServletAutoConfiguration {   /*    * The bean name for a DispatcherServlet that will be mapped to the root URL 将映射到根URL的DispatcherServlet的bean名称"/"    */   public static final String DEFAULT_DISPATCHER_SERVLET_BEAN_NAME = "dispatcherServlet";   /*    * The bean name for a ServletRegistrationBean for the DispatcherServlet DispatcherServlet的ServletRegistrationBean的bean名称"/"    */   public static final String DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME = "dispatcherServletRegistration";   @Configuration   @Conditional(DefaultDispatcherServletCondition.class)   @ConditionalOnClass(ServletRegistration.class)   @EnableConfigurationProperties(WebMvcProperties.class)   protected static class DispatcherServletConfiguration {      private final WebMvcProperties webMvcProperties;      public DispatcherServletConfiguration(WebMvcProperties webMvcProperties) {         this.webMvcProperties = webMvcProperties;      }//    初始化dispatcherServlet      @Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)      public DispatcherServlet dispatcherServlet() {         DispatcherServlet dispatcherServlet = new DispatcherServlet();         dispatcherServlet.setDispatchOptionsRequest(               this.webMvcProperties.isDispatchOptionsRequest());         dispatcherServlet.setDispatchTraceRequest(               this.webMvcProperties.isDispatchTraceRequest());         dispatcherServlet.setThrowExceptionIfNoHandlerFound(               this.webMvcProperties.isThrowExceptionIfNoHandlerFound());         return dispatcherServlet;      }//    初始化multipartResolver      @Bean      @ConditionalOnBean(MultipartResolver.class)      @ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)      public MultipartResolver multipartResolver(MultipartResolver resolver) {         // Detect if the user has created a MultipartResolver but named it incorrectly 检测用户是否创建了一个多部件解析器,但命名不正确         return resolver;      }   }   @Configuration   @Conditional(DispatcherServletRegistrationCondition.class)   @ConditionalOnClass(ServletRegistration.class)   @EnableConfigurationProperties(WebMvcProperties.class)   @Import(DispatcherServletConfiguration.class)   protected static class DispatcherServletRegistrationConfiguration {      private final ServerProperties serverProperties;      private final WebMvcProperties webMvcProperties;      private final MultipartConfigElement multipartConfig;      public DispatcherServletRegistrationConfiguration(            ServerProperties serverProperties, WebMvcProperties webMvcProperties,            ObjectProvider
multipartConfigProvider) { this.serverProperties = serverProperties; this.webMvcProperties = webMvcProperties; this.multipartConfig = multipartConfigProvider.getIfAvailable(); }// 初始化dispatcherServletRegistration @Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME) @ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME) public ServletRegistrationBean dispatcherServletRegistration( DispatcherServlet dispatcherServlet) {// mapping默认是/* ServletRegistrationBean registration = new ServletRegistrationBean( dispatcherServlet, this.serverProperties.getServletMapping()); registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME); registration.setLoadOnStartup( this.webMvcProperties.getServlet().getLoadOnStartup()); if (this.multipartConfig != null) { registration.setMultipartConfig(this.multipartConfig); } return registration; } } @Order(Ordered.LOWEST_PRECEDENCE - 10) private static class DefaultDispatcherServletCondition extends SpringBootCondition { @Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { ConditionMessage.Builder message = ConditionMessage .forCondition("Default DispatcherServlet"); ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();// 根据类型从beanFactory找出DispatcherServlet实现类的beanNames List
dispatchServletBeans = Arrays.asList(beanFactory .getBeanNamesForType(DispatcherServlet.class, false, false)); if (dispatchServletBeans.contains(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)) { return ConditionOutcome.noMatch(message.found("dispatcher servlet bean") .items(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)); } if (beanFactory.containsBean(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)) { return ConditionOutcome .noMatch(message.found("non dispatcher servlet bean") .items(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)); } if (dispatchServletBeans.isEmpty()) { return ConditionOutcome .match(message.didNotFind("dispatcher servlet beans").atAll()); } return ConditionOutcome.match(message .found("dispatcher servlet bean", "dispatcher servlet beans") .items(Style.QUOTE, dispatchServletBeans) .append("and none is named " + DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)); } } @Order(Ordered.LOWEST_PRECEDENCE - 10) private static class DispatcherServletRegistrationCondition extends SpringBootCondition { @Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); ConditionOutcome outcome = checkDefaultDispatcherName(beanFactory); if (!outcome.isMatch()) { return outcome; } return checkServletRegistration(beanFactory); } private ConditionOutcome checkDefaultDispatcherName( ConfigurableListableBeanFactory beanFactory) { List
servlets = Arrays.asList(beanFactory .getBeanNamesForType(DispatcherServlet.class, false, false)); boolean containsDispatcherBean = beanFactory .containsBean(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME); if (containsDispatcherBean && !servlets.contains(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)) { return ConditionOutcome .noMatch(startMessage().found("non dispatcher servlet") .items(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)); } return ConditionOutcome.match(); } private ConditionOutcome checkServletRegistration( ConfigurableListableBeanFactory beanFactory) { ConditionMessage.Builder message = startMessage();// 从beanFactory中查找ServletRegistrationBean的实现类的beanNames List
registrations = Arrays.asList(beanFactory .getBeanNamesForType(ServletRegistrationBean.class, false, false)); boolean containsDispatcherRegistrationBean = beanFactory .containsBean(DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME); if (registrations.isEmpty()) { if (containsDispatcherRegistrationBean) { return ConditionOutcome .noMatch(message.found("non servlet registration bean").items( DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)); } return ConditionOutcome .match(message.didNotFind("servlet registration bean").atAll()); } if (registrations .contains(DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)) { return ConditionOutcome.noMatch(message.found("servlet registration bean") .items(DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)); } if (containsDispatcherRegistrationBean) { return ConditionOutcome .noMatch(message.found("non servlet registration bean").items( DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)); } return ConditionOutcome.match(message.found("servlet registration beans") .items(Style.QUOTE, registrations).append("and none is named " + DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)); } private ConditionMessage.Builder startMessage() { return ConditionMessage.forCondition("DispatcherServlet Registration"); } }}

 

说在最后

本次仅代表个人观点,仅供参考。

 

转载于:https://my.oschina.net/u/3775437/blog/3029549

你可能感兴趣的文章
htm5新特性(转)
查看>>
Linux-Centos启动流程
查看>>
php 设计模式
查看>>
后端技术精选 - 收藏集 - 掘金
查看>>
Laravel 服务容器
查看>>
mac安装kubernetes并运行echoserver
查看>>
多页架构的前后端分离方案(webpack+express)
查看>>
算法(第4版) Chapter 1
查看>>
前端技术选型的遗憾和经验教训
查看>>
“亲切照料”下的领域驱动设计
查看>>
SRE工程师到底是做什么的?
查看>>
解读:Red Hat为什么收购Ansible
查看>>
PHP json_encode() 函数介绍
查看>>
js动态设置元素高度
查看>>
Ossim下的安全合规管理
查看>>
DelphiWebMVC框架下BPL热部署实现
查看>>
C++与MySQL的冲突
查看>>
siki学习之观察者模式笔记
查看>>
单元测试
查看>>
spring.net 继承
查看>>