配置 Servlet
有两种方式可以实现这个需求:
- 配置文件中,以:server 开头的配置就是对 Servlet 容器生效,其中还包含了 Tomcat 的配置。
server.port=8090
server.tomcat.uri-encoding=UTF-8
- 实现一个嵌入式的 Servlet 定制器
- SpringBoot 2.x 的版本与 1.x 的不同
- 需要用到两个接口:WebServerFactoryCustomizer、ConfigurableWebServerFactory
- 前者是可以在服务器启动之前,设置一个基本配置;
- 后者是一个可以配置的 WebServerFactory 子接口,实际上这个接口本身没有任何方法,依靠子接口 ConfigurableWebServerFactory 的扩展才有特定的方法需要实现
只要实现了按规则的定制器,接下来会由内置的 Server 容器给其注入实现了 WebServerFactoryCustomizer、ConfigurableWebServerFactory 两个接口的对象。
2.x
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory > webServerFactoryCustomizer() {
return new WebServerFactoryCustomizer<ConfigurableWebServerFactory >() {
@Override
public void customize(ConfigurableWebServerFactory factory) {
factory.setPort(8090);
}
};
}
1.x
@Bean
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(8090);
}
};
}
WebServerFactoryCustomizer 层级结构图
ConfigurableWebServerFacotry 层级结构图
之所以 Customizer 方法能生效,在 ServletWebServerFactoryAutoConfiguration 类里边,注册了一个 WebServerFactoryCustomizerBeanPostProcessor bean 后置处理器组件,后置处理器会由 SpringBoot 统一加载,该组件会获取所有实现 WebServerFactoryCustomizer 接口的类,并且遍历加载及执行 customize 方法。
/**
1.1 获取所有 Customizers
*/
private Collection<WebServerFactoryCustomizer<?>> getCustomizers() {
if (this.customizers == null) {
// Look up does not include the parent context
this.customizers = new ArrayList<>(getWebServerFactoryCustomizerBeans());
this.customizers.sort(AnnotationAwareOrderComparator.INSTANCE);
this.customizers = Collections.unmodifiableList(this.customizers);
}
return this.customizers;
}
/**
1.2 获取容器中所有满足该类型的 WebServerFactoryCustomizer 的 bean 组件
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private Collection<WebServerFactoryCustomizer<?>> getWebServerFactoryCustomizerBeans() {
return (Collection) this.beanFactory.getBeansOfType(WebServerFactoryCustomizer.class, false, false).values();
}
/**
1.3 加载并且执行所有 Customizer 类的 customize 方法
*/
@SuppressWarnings("unchecked")
private void postProcessBeforeInitialization(WebServerFactory webServerFactory) {
LambdaSafe.callbacks(WebServerFactoryCustomizer.class, getCustomizers(), webServerFactory)
.withLogger(WebServerFactoryCustomizerBeanPostProcessor.class)
.invoke((customizer) -> customizer.customize(webServerFactory));
}
注册 Serlvet、Filter、Listener 组件
这三大组件只是注册方式与 Spring 在形式上略有不同,本质上,还是要注册到 Bean,需要的时候转发到这个组件。
所以,在一些设置上,没有不同的。
注册 Servlet 组件
要想注册 Serlvet 组件,需要使用到:ServletRegistartionBean 类,该类就作为 bean,并且设置 Serlvet 组件,以及路由映射都在这个类中设置。
@SpringBootConfiguration
public class CustomSerlvetBean {
@Bean
public ServletRegistrationBean<CustomSerlvet> servletRegistrationBean() {
ServletRegistrationBean<CustomSerlvet> servletRegistrationBean = new ServletRegistrationBean<>();
servletRegistrationBean.setServlet(new CustomSerlvet());
servletRegistrationBean.addUrlMappings("/123");
return servletRegistrationBean;
}
}
Serlvet 实现类,ServletRegistrationBean 接受的参数必须是要实现 Servlet 接口,再不济也是子类或者子接口。那么,这里会继承 HttpSerlvet 类来实现 doGet、doPost 方法的处理。
public class CustomSerlvet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(getClass().getName() + "doGet");
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(getClass().getName() + "doPost");
}
}
以上两个类的操作,就满足了 SpringBoot 对自定义信息的处理,尤其是添加了 @SpringBootConfiguration 和 @Bean 两个注解,SpringBoot 会自动的注册这些 Bean。
注册 Filter 组件
需要注册一个 Bean FilterRegistrationBean 类,然后通过这个类来只当过滤器,可以选择过滤哪些 url,或者保持默认则 /** 都会被拦截过滤。
@SpringBootConfiguration
public class CustomFilterBean {
@Bean
public FilterRegistrationBean<CustomFilter> filterRegistrationBean() {
FilterRegistrationBean<CustomFilter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
filterFilterRegistrationBean.setFilter(new CustomFilter());
return filterFilterRegistrationBean;
}
}
Filter 组件的实现,可以实现 Filter 接口,当然可以继承这个 HttpFilter 类,本身这个类也是实现了接口的。
public class CustomFilter extends HttpFilter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println(getClass().getName() + "doFilter");
chain.doFilter(request, response);
}
}
注册 Listener 组件
实现方式还是一样,对于 Listener 的组件,有好几种监听接口:
- ServletContextAttributeListener
- ServletRequestListener
- ServletRequestAttributeListener
- HttpSessionAttributeListener
- HttpSessionListener
- ServletContextListener
在 ServletListenerRegistrationBean 类中,初始化了六种监听接口,这些接口分别对项目的不同操作进行监听,例如说 Session;
public class ServletListenerRegistrationBean<T extends EventListener> extends RegistrationBean {
private static final Set<Class<?>> SUPPORTED_TYPES;
static {
Set<Class<?>> types = new HashSet<>();
types.add(ServletContextAttributeListener.class);
types.add(ServletRequestListener.class);
types.add(ServletRequestAttributeListener.class);
types.add(HttpSessionAttributeListener.class);
types.add(HttpSessionListener.class);
types.add(ServletContextListener.class);
SUPPORTED_TYPES = Collections.unmodifiableSet(types);
}
// ......
}
同样需要注册一个 Bean,通过 ServletListenerRegistrationBean 这个类可以往添加一个监听器。
@SpringBootConfiguration
public class CustomListenerBean {
@Bean
public ServletListenerRegistrationBean<CustomListener> listenerRegistrationBean() {
ServletListenerRegistrationBean<CustomListener> bean = new ServletListenerRegistrationBean<>();
bean.setListener(new CustomListener());
return bean;
}
}
同时,这里选择 ServletContextListener 作为范例:
public class CustomListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Servlet 启动的时候初始化......");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("Servlet 容器销毁的时候......");
}
}
这里两个方法是在 Servlet 启动与销毁的时候会触发。
Servlet 启动的时候初始化......
2020-05-26 09:51:38.758 INFO 10936 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-05-26 09:51:38.823 INFO 10936 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index
2020-05-26 09:51:38.960 INFO 10936 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8090 (http) with context path ''
2020-05-26 09:51:38.965 INFO 10936 --- [ main] c.huasio.learning.demo.DemoApplication : Started DemoApplication in 2.777 seconds (JVM running for 4.221)
2020-05-26 09:51:41.955 INFO 10936 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
Servlet 容器销毁的时候......
其他 Servlet 容器
想要切换其他的 Servlet 容器,就得知道,SpringBoot 默认支持哪些容器。
可以通过在 ServerProperties 配置类中知道支持的类型。
SpringBoot 默认的 Serlvet 容器是 Tomcat,通过分析依赖,可以看到,能使用 Tomcat 容器,就是因为 Web
Starter 引入了 Tomcat 的 starter;
也就是说,通过更换 starter 就可以平滑的更换 Servlet 容器。
接下来,只需要排除 tomcat 依赖,然后引入需要的 Servlet 容器的 starter。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- 排除 Tomcat starter -->
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入 Undertow starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
之后,就可以直接启动使用了。
若是需要配置,可以在配置文件中,也可以注册一个 WebServerFactoryCustomizer 定制器的 bean,通过这个类可以对特定容器的定制化设置。
嵌入式 Servlet 自动加载原理
通过根据 SpringBoot 程序的启动步骤,发现了 SpringBoot 会根据不同的应用程序类型,来加载不同的 WebServerApplicationContext 后缀的文件,总结一下,对于 Web 应用的有两种:
-
基于 Servlet 的 Web 应用,SpringBoot 默认采用这种类型
-
org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext
-
-
反应性(Reactive)的 Web 应用,也就是 Spring 5.0 新增的一个模块,非阻塞式 Web 框架与 Servlet 使用不同的接口
-
org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext
-
两种类型分别对应两个 WebServerApplicationContext 文件,这两个文件会对嵌入式 Web 服务器的作不同的处理。
public class SpringApplication {
public static final String DEFAULT_CONTEXT_CLASS = "org.springframework.context."
+ "annotation.AnnotationConfigApplicationContext";
public static final String DEFAULT_SERVLET_WEB_CONTEXT_CLASS = "org.springframework.boot."
+ "web.servlet.context.AnnotationConfigServletWebServerApplicationContext";
public static final String DEFAULT_REACTIVE_WEB_CONTEXT_CLASS = "org.springframework."
+ "boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext";
// ...... 省略一些属性
protected ConfigurableApplicationContext createApplicationContext() {
Class<?> contextClass = this.applicationContextClass;
if (contextClass == null) {
try {
switch (this.webApplicationType) {
case SERVLET:
contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
break;
case REACTIVE:
contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
break;
default:
contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
}
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Unable create a default ApplicationContext, please specify an ApplicationContextClass", ex);
}
}
return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}
}
判断出符合条件的 Web 类型之后,通过反射的方式,实例化这个类,同时会将这个应用上下文实例返回,作为后续启动嵌入式环境的主要类。
public ConfigurableApplicationContext run(String... args) {
// ...... 省略
try {
// ...... 省略
context = createApplicationContext();
// ...... 省略
refreshContext(context);
// ...... 省略
return context;
}
}
跟着一路下来,最后会调用 ServletWebServerApplicationContext 这个类的 onRefresh 方法。
通过上下文可以知道,context 本质就是 AnnotationConfigServletWebServerApplicationContext 类的实例化,但是这个也是作为 ServletWebServerApplicationContext 子类存在。
AnnotationConfigServletWebServerApplicationContext 没有重写的方法会调用父类。
@Override
protected void onRefresh() {
super.onRefresh();
try {
createWebServer();
}
catch (Throwable ex) {
throw new ApplicationContextException("Unable to start web server", ex);
}
}
最终定位到:ServletWebServerApplicationContext 类的 createWebServer 方法,
private void createWebServer() {
WebServer webServer = this.webServer;
ServletContext servletContext = getServletContext();
if (webServer == null && servletContext == null) {
ServletWebServerFactory factory = getWebServerFactory();
this.webServer = factory.getWebServer(getSelfInitializer());
}
else if (servletContext != null) {
try {
getSelfInitializer().onStartup(servletContext);
}
catch (ServletException ex) {
throw new ApplicationContextException("Cannot initialize servlet context", ex);
}
}
initPropertySources();
}
通过这一小段的代码明显可以得知,通过获取到项目中当前的嵌入式 Web 服务器,来执行相应的操作。
以 Tomcat 为例,最终,Tomcat 会执行这段代码。
@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {
if (this.disableMBeanRegistry) {
Registry.disableRegistry();
}
Tomcat tomcat = new Tomcat();
File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat");
tomcat.setBaseDir(baseDir.getAbsolutePath());
Connector connector = new Connector(this.protocol);
connector.setThrowOnFailure(true);
tomcat.getService().addConnector(connector);
customizeConnector(connector);
tomcat.setConnector(connector);
tomcat.getHost().setAutoDeploy(false);
configureEngine(tomcat.getEngine());
for (Connector additionalConnector : this.additionalTomcatConnectors) {
tomcat.getService().addConnector(additionalConnector);
}
prepareContext(tomcat.getHost(), initializers);
return getTomcatWebServer(tomcat);
}
之所以会找到 Tocmat 的工厂类,在之前已经注册到容器中了。SpringBoot 内置有一个专门自动配 Servlet 服务器的自动配置类。
@Configuration(proxyBeanMethods = false)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnClass(ServletRequest.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
@EnableConfigurationProperties(ServerProperties.class)
@Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class,
ServletWebServerFactoryConfiguration.EmbeddedTomcat.class,
ServletWebServerFactoryConfiguration.EmbeddedJetty.class,
ServletWebServerFactoryConfiguration.EmbeddedUndertow.class })
public class ServletWebServerFactoryAutoConfiguration {
// ......
}
标注有 @Import 注解的类,将会导入指定的组件。刚好,SpringBoot 支持三个嵌入式服务器都在。
这个类里面就有三个标注 @Bean 的静态内部类。
所以为什么在 createWebServer 这个位置可以正常的获取到嵌入式服务器工厂容器。
嵌入式 Servlet 启动原理
- 由主程序类中起步,进入 SpringApplicaiton 类中,在这个类里面,最终会调用该类,创建 ioc 容器,以及刷新 ioc 容器。
public ConfigurableApplicationContext run(String... args) {
// ......
}
- 进入刷新 ioc 容器逻辑中,一直到调用 AbstractApplicationContext 的 refresh 方法,这里有一个步,就是调用 onRefresh 方法,该方法由子类提供。
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
- 在 onRefresh 方法中,会创建 WebServer,当然是调用提供的方法。方法中会获取 ioc 容器,同时获取 Web 服务器工厂,通过该工厂类来创建 Web 服务器。整个 Web 服务器的启动大概过程就是这样,启动具体逻辑由各个 Web 服务器特定,所以到这里就完成启动了。
@Override
protected void onRefresh() {
super.onRefresh();
try {
createWebServer();
}
catch (Throwable ex) {
throw new ApplicationContextException("Unable to start web server", ex);
}
}
private void createWebServer() {
WebServer webServer = this.webServer;
ServletContext servletContext = getServletContext();
if (webServer == null && servletContext == null) {
ServletWebServerFactory factory = getWebServerFactory();
this.webServer = factory.getWebServer(getSelfInitializer());
}
else if (servletContext != null) {
try {
getSelfInitializer().onStartup(servletContext);
}
catch (ServletException ex) {
throw new ApplicationContextException("Cannot initialize servlet context", ex);
}
}
initPropertySources();
}
细节补充:
关于 Customizer 定制器何时加载?
会在 Web 服务器启动之前就被加载,由特定的 Web 服务器工厂定制器的 bean 后置处理器类负责加载这些 Customizer 定制器。
总的来说,以 Tomcat 为例,默认的处理器会加载三个:ServletWebServerFactoryAutoConfiguration 有两个:
@Bean
public ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer(ServerProperties serverProperties) {
return new ServletWebServerFactoryCustomizer(serverProperties);
}
@Bean
@ConditionalOnClass(name = "org.apache.catalina.startup.Tomcat")
public TomcatServletWebServerFactoryCustomizer tomcatServletWebServerFactoryCustomizer(
ServerProperties serverProperties) {
return new TomcatServletWebServerFactoryCustomizer(serverProperties);
}
EmbeddedWebServerFactoryCustomizerAutoConfiguration 类里边有一个,这个是处理用户自定义 Customizer 的最后加载。所以这里边的设置可能会覆盖前面的 Customizer 相同设置。于此同理,后面的用户自定义的 Customizer 定制器也会覆盖默认的这些定制器。
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ Tomcat.class, UpgradeProtocol.class })
public static class TomcatWebServerFactoryCustomizerConfiguration {
@Bean
public TomcatWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer(Environment environment,
ServerProperties serverProperties) {
return new TomcatWebServerFactoryCustomizer(environment, serverProperties);
}
}
使用外部 Servlet 容器
创建要给以外部 Servlet 容器的 Web 项目,需要几个步骤:
- 创建 SpringBoot 项目必须是 war 类型
- 创建 webapp 目录,作为 Web 项目的静态资源目录以及 web.xml 配置文件
- 在 IDEA 中,配置外部服务器来运行项目,例如 Tomcat,然后指定项目运行
这样的步骤就足够了,可以正常运行。
需要注意一点,与主程序类同级必须要有一个实现 SpringBootServletInitializer 类的类,否则是无法启动外部服务器。
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
}
原理
在 Servlet 3.1 规范中,有这样一条规范:
1)服务器启动的时候,会创建当前 web 应用里面每一个 jar 包的 ServletContainerInitializer 实例
2) ServletContainerInitializer 的实现放在 jar 包的 META-INF/services 文件夹下面,有一个名为:javax.servlet.ServletContainerInitializer 的文件,内容就是 ServletContainerInitializer 的实现类全类名
3) 除此之外,还有使用 @HandlesTypes 注解,在应用启动的时候加载感兴趣的类。
启动流程:
1)启动 Tomcat 的时候,会在 org\springframework\spring-web\5.2.6.RELEASE\spring-web-5.2.6.RELEASE.jar!\META-INF\services\javax.servlet.ServletContainerInitializer 加载实现类
2)SpringServletContainerInitializer 类标注了 @HandleTypes 注解,将所有符合 WebApplicationInitializer 类型的类,都加载进来,同时会注入到 onStartup 方法参数中 Set<Class<?>> webAppInitializerClasses
@Override
public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)
throws ServletException {
List<WebApplicationInitializer> initializers = new LinkedList<>();
if (webAppInitializerClasses != null) {
for (Class<?> waiClass : webAppInitializerClasses) {
// 排除接口以及抽象类,剩下的就是要实例化
if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) &&
WebApplicationInitializer.class.isAssignableFrom(waiClass)) {
try {
initializers.add((WebApplicationInitializer)
ReflectionUtils.accessibleConstructor(waiClass).newInstance());
}
catch (Throwable ex) {
throw new ServletException("Failed to instantiate WebApplicationInitializer class", ex);
}
}
}
}
if (initializers.isEmpty()) {
servletContext.log("No Spring WebApplicationInitializer types detected on classpath");
return;
}
servletContext.log(initializers.size() + " Spring WebApplicationInitializers detected on classpath");
AnnotationAwareOrderComparator.sort(initializers);
for (WebApplicationInitializer initializer : initializers) {
initializer.onStartup(servletContext);
}
}
3)注入到 onStartup 方法的 WebApplicationInitializer 类型的类,会被遍历;排除接口以及抽象类,剩下的都会被一一实例化。
4)最后将实例化后的 WebApplicationInitializer 类,一一调用它们的 onStartup 方法。
5)项目自带的 com.example.demo.ServletInitializer 类会被加载,同时会调用其父类的方法 onStartup 对项目进行启动。
public void onStartup(ServletContext servletContext) throws ServletException {
this.logger = LogFactory.getLog(this.getClass());
WebApplicationContext rootAppContext = this.createRootApplicationContext(servletContext);
if (rootAppContext != null) {
servletContext.addListener(new ContextLoaderListener(rootAppContext) {
public void contextInitialized(ServletContextEvent event) {
}
public void contextDestroyed(ServletContextEvent event) {
try {
super.contextDestroyed(event);
} finally {
SpringBootServletInitializer.this.deregisterJdbcDrivers(event.getServletContext());
}
}
});
} else {
this.logger.debug("No ContextLoaderListener registered, as createRootApplicationContext() did not return an application context");
}
}
6)在这个方法中,会创建根容器,同时也会启动容器。
protected WebApplicationContext createRootApplicationContext(ServletContext servletContext)
// 创建一个可用于配置和构建 SpringApplication 构建器类
SpringApplicationBuilder builder = createSpringApplicationBuilder();
// 设置 main 主类
builder.main(getClass());
ApplicationContext parent = getExistingRootWebApplicationContext(servletContext);
if (parent != null) {
this.logger.info("Root context already created (using as parent).");
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null);
builder.initializers(new ParentContextApplicationContextInitializer(parent));
}
// 向应用程序添加一些初始化器(在加载任何bean定义之前应用于 ApplicationContext)。
builder.initializers(new ServletContextApplicationContextInitializer(servletContext));
builder.contextClass(AnnotationConfigServletWebServerApplicationContext.class);
// 调用 ServletInitializer 类的 configure 方法
// 向这个应用程序添加更多的源(配置类和组件)。
builder = configure(builder);
builder.listeners(new WebEnvironmentPropertySourceInitializer(servletContext));
// 构建一个 ioc 容器
SpringApplication application = builder.build();
if (application.getAllSources().isEmpty()
&& MergedAnnotations.from(getClass(), SearchStrategy.TYPE_HIERARCHY).isPresent(Configuration.class)) {
application.addPrimarySources(Collections.singleton(getClass()));
}
Assert.state(!application.getAllSources().isEmpty(),
"No SpringApplication sources have been defined. Either override the "
+ "configure method or add an @Configuration annotation");
// Ensure error pages are registered
if (this.registerErrorPageFilter) {
application.addPrimarySources(Collections.singleton(ErrorPageFilterConfiguration.class));
}
application.setRegisterShutdownHook(false);
// 运行 ioc SpringBoot 项目
return run(application);
}
7)这个方法最后会运行 SpringApplicatin run 的方法,最后还是回归到原本的流程。
总结
Servlet 自动加载原理
- SpringBoot 根据满足条件的情况,来加载不同的 Web 模块,默认加载基于 Servlet 的 Web 程序
- 调用所有 WebServerFactoryCustomizerBeanPostProcessor 后置处理器,将所有 WebServerFactoryCustomizer 类型的类都其 customize 方法,设置 Server 默认基本配置
- 获取 ServletWebServerFactory 服务器工厂类,然后通过这个实力调用指定的服务器容器执行相应操作,启动服务器。
相关类
名称 | 类型 | 描述 |
---|---|---|
WebServerFactory | 接口 | 用于创建 Web 服务器工厂的标记接口。 这是要给空接口,没有任何方法的。 实现的内容都在子接口扩展。 |
ConfigurableWebServerFactory | 接口 | 一个可配置的 WebServerFactory 子接口。提供几个基本的方法,这些方法实现类去实现。 |
WebServerFactoryCustomizer | 接口 | 用于自定义 WebServerFactory Web 服务器工厂的策略接口。 任何实现该接口的或者间接实现、继承等所有属于该类型的 bean 都会在服务器启动之前从服务器工厂获得回调; 通过这些回调可以设置服务器的端口、地址、错误页面等设置。 |
ServletWebServerFactoryAutoConfiguration | 类 | Servlet Web 服务器工厂的自动配置 |
ServletWebServerFactoryConfiguration | 类 | Servlet Web 服务器的配置类,SpringBoot 支持的嵌入式服务器都在这个类,注册满足条件的 bean,实际上就是对不同服务器容器的注册到容器中。 |
ServletWebServerApplicationContext | 类 | SpringBoot 上下文环境类,也就是 ioc 容器。但是使用的还不是这个对象的实例,而是其子类的实例。 |
AnnotationConfigServletWebServerApplicationContext | 类 | ioc 容器类,继承 ServletWebServerApplicationContext 类,是基于 Servlet API 的 Web 应用程序使用的 ioc 容器。 |
AnnotationConfigReactiveWebServerApplicationContext | 类 | Spring 5.0 Web 新模块,Reactive 的 Web 应用程序,也就是响应式。在这个环境下,使用的是这个对象的 ioc 实例。 |
AnnotationConfigApplicationContext | 类 | 默认的 ioc 容器,非 Web 程序运行方式。 |
WebServerFactoryCustomizerBeanPostProcessor | 类 | Web服务器的工厂定制器 bean 后置处理器。 |
以 WebServerFactory 结尾的表示这是一个 Web 服务器工厂类,单纯的接口只是一个标记接口,而其子接口及其实现类是 Web 服务器的类,例如说 Tomcat、Undertow、Jetty。
以 Customizer 结尾的表示这是一个定制器,用于对服务器进行基本设置的相关类。
以 WebServerApplicationContext 结尾的表示这是一个 Spring 上下文环境类,也就是 ioc。
以 AutoConfiguration 结尾的表示这是一个自动配置类,由 SpringBoot 自动加载并且注册相应 bean。
以 Configuration 结尾的表示这是一个配置类,单纯的配置类
Q.E.D.
Comments | 0 条评论