返回顶部
首页 > 资讯 > 后端开发 > Python >Springboot自动装配之注入DispatcherServlet的实现方法
  • 445
分享到

Springboot自动装配之注入DispatcherServlet的实现方法

2024-04-02 19:04:59 445人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

原理概述 SpringBoot向外界提供WEB服务,底层依赖了springframework中的web模块(包含但不限于spring mvc核心类DispatcherServlet)

原理概述

SpringBoot向外界提供WEB服务,底层依赖了springframework中的web模块(包含但不限于spring mvc核心类DispatcherServlet)来实现

那么springboot在什么时机向容器注入DispatcherServlet这个核心类的呢
注入的流程还是遵循了自动装配流程,在springboot框架里默认提供了该自动装配的支持

jar包里的spring.factories文件里有个 org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration配置

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(DispatcherServlet.class)
@AutoConfigureAfter(ServletWebServerFactoryAutoConfiguration.class)
public class DispatcherServletAutoConfiguration {
	
	public static final String DEFAULT_DISPATCHER_SERVLET_BEAN_NAME = "dispatcherServlet";
	/*
	 * The bean name for a ServletReGIStrationBean for the DispatcherServlet "/"
	 *
	public static final String DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME = "dispatcherServletRegistration";
	// ... 省略代码
	}

在DispatcherServletAutoConfiguration中有两个内部类(配置类) DispatcherServletConfiguration、DispatcherServletRegistrationConfiguration和两个对应的Condition类DefaultDispatcherServletCondition、DispatcherServletRegistrationCondition

DispatcherServletConfiguration

DispatcherServletConfiguration是DispatcherServletAutoConfiguration 的一个内部类,并被 @Configuration标注,会被容器自动扫描到;其有两个方法: 1、dispatcherServlet;2、multipartResolver

	@Configuration(proxyBeanMethods = false)
	@Conditional(DefaultDispatcherServletCondition.class)
	@ConditionalOnClass(ServletRegistration.class)
	@EnableConfigurationProperties({ HttpProperties.class, WebMvcProperties.class })
	protected static class DispatcherServletConfiguration {

// @A
		@Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
		public DispatcherServlet dispatcherServlet(HttpProperties httpProperties, WebMvcProperties webMvcProperties) {
			DispatcherServlet dispatcherServlet = new DispatcherServlet();
			dispatcherServlet.setDispatchOptionsRequest(webMvcProperties.isDispatchOptionsRequest());
			dispatcherServlet.setDispatchTraceRequest(webMvcProperties.isDispatchTraceRequest());
			dispatcherServlet.setThrowExceptionIfNoHandlerFound(webMvcProperties.isThrowExceptionIfNoHandlerFound());
			dispatcherServlet.setPublishEvents(webMvcProperties.isPublishRequestHandledEvents());
			dispatcherServlet.setEnableLoggingRequestDetails(httpProperties.isLogRequestDetails());
			return dispatcherServlet;
		}

// @B
		@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;
		}
	}

@A:

  • @Configuration(proxyBeanMethods = false):配置类采用Lite模式
  • @Conditional(DefaultDispatcherServletCondition.class):根据DefaultDispatcherServletCondition类返回值计算是否要注入,主要逻辑是检验Spring容器中是否已经存在一个名字为"dispatcherServlet"的DispatcherServlet
  • @ConditionalOnClass(ServletRegistration.class):容器中必须要有ServletRegistration类
  • @EnableConfigurationProperties({ HttpProperties.class, WebMvcProperties.class }):加载HttpProperties和WebMvcProperties

@B:注入 MultipartResolver 解析类

DispatcherServletRegistrationConfiguration

该类注入的逻辑与DispatcherServletConfiguration 相同,只不过在类上注解了 @Import(DispatcherServletConfiguration.class)那么这两个类定义就有了先后顺序

@Configuration(proxyBeanMethods = false)
	@Conditional(DispatcherServletRegistrationCondition.class)
	@ConditionalOnClass(ServletRegistration.class)
	@EnableConfigurationProperties(WebMvcProperties.class)
	@Import(DispatcherServletConfiguration.class) //@A
	protected static class DispatcherServletRegistrationConfiguration {

		@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
		@ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
		public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet,
				WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfig) {
			DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet,
					webMvcProperties.getServlet().getPath());
			// 设置名称		
			registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
			registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
			multipartConfig.ifAvailable(registration::setMultipartConfig);
			return registration;
		}
	}

其中,DispatcherServletRegistrationBean继承自ServletRegistrationBean,主要为DispatcherServlet提供服务。DispatcherServletRegistrationBean和DispatcherServlet都提供了注册Servlet并公开DispatcherServletPath信息的功能

面试

spring mvc和Spring Boot的关系,相信读了上面的文章会说出一两点;

到此这篇关于Springboot自动装配之注入DispatcherServlet的文章就介绍到这了,更多相关Springboot注入DispatcherServlet内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Springboot自动装配之注入DispatcherServlet的实现方法

本文链接: https://lsjlt.com/news/149280.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作