一、内置Servlet容器
1.1 定制和修改Servlet容器配置
(1)在配置文件 application.properties/yaml 中修改。示例:
server.port=8081
#项目路径
server.servlet.context-path=/dmf
server.tomcat.uri-encoding=UTF-8
#通用的Servlet容器配置
server.xxx
#Tomcat的设置
server.tomcat.xxx
这些配置对应的是 ServerProperties
,这是个配置类,对应的就是服务的相关配置。
(2)代码里设置。
Spring boot1.x 和 Spring Boot2.x 的配置方法有些不同。
SpringBoot1.x 定制和修改Servlet容器的相关配置示例:
@Configuration
public class MyConfig {
@Bean //必须要加到容器中才能生效
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
//在这里修改相关配置
container.setPort(8088);
}
};
}
}
SpringBoot2.x定制和修改Servlet容器的相关配置示例:
@Configuration
public class MyConfig {
@Bean //必须要加到容器中才能生效
public ConfigurableServletWebServerFactory configurableServletWebServerFactory() {
//修改tomcat相关配置
TomcatServletWebServerFactory factory= new TomcatServletWebServerFactory();
factory.setPort(8083);
return factory;
}
//或者使用下面这种方法,泛型根据具体的servlet容器来写。
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> webServerFactoryCustomizer() {
return new WebServerFactoryCustomizer<TomcatServletWebServerFactory>() {
@Override
public void customize(TomcatServletWebServerFactory factory) {
factory.setPort(8084);
}
};
}
}
上面这两种方法二选一,原理都是一样的。
1.2 切换内置Servlet容器
Spring Boot默认使用tomcat作为Servlet容器,同时也支持Jetty、Undertow等内置Sevlet容器,可以根据需要切换相应的Servlet容器。
步骤:
1、在web-Starter里排除tomcat的依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
2、导入其他的Servlet容器依赖。
<!--引入Jetty-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<!--引入Undertow-->
<dependency>
<artifactId>spring-boot-starter-undertow</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
1.3、内置Servlet容器原理
1.3.1 自动配置原理(2.x版本)
这里以2.x版本中的tomcat为例,找到内置Servlet容器的自动配置类,在spring-boot-autoconfigure.jar中的 ServletWebServerFactoryAutoConfiguration 和 EmbeddedWebServerFactoryCustomizerAutoConfiguration。
@Configuration
@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 {
//往容器中导入了ServletWebServerFactoryCustomizer
//servletWeb服务工程定制器
@Bean
public ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer(
ServerProperties serverProperties) {
return new ServletWebServerFactoryCustomizer(serverProperties);
}
//导入了TomcatServletWebServerFactoryCustomizer
@Bean
@ConditionalOnClass(name = "org.apache.catalina.startup.Tomcat")//当容器中有Tomcat类就生效
public TomcatServletWebServerFactoryCustomizer tomcatServletWebServerFactoryCustomizer(
ServerProperties serverProperties) {
return new TomcatServletWebServerFactoryCustomizer(serverProperties);
}
//这个类的作用是往容器中添加一些组件
public static class BeanPostProcessorsRegistrar
implements ImportBeanDefinitionRegistrar, BeanFactoryAware {
private ConfigurableListableBeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
if (beanFactory instanceof ConfigurableListableBeanFactory) {
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
}
}
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
BeanDefinitionRegistry registry) {
if (this.beanFactory == null) {
return;
}
//往容器中添加了WebServerFactoryCustomizerBeanPostProcessor组件(web服务工厂定制器的后置处理器)。
//这个类实现了BeanPostProcessor,属于bean的后置处理器。作用是在bean初始化前后加一些自己的逻辑处理
registerSyntheticBeanIfMissing(registry,
"webServerFactoryCustomizerBeanPostProcessor",
WebServerFactoryCustomizerBeanPostProcessor.class);
registerSyntheticBeanIfMissing(registry,
"errorPageRegistrarBeanPostProcessor",
ErrorPageRegistrarBeanPostProcessor.class);
}
private void registerSyntheticBeanIfMissing(BeanDefinitionRegistry registry,
String name, Class<?> beanClass) {
if (ObjectUtils.isEmpty(
this.beanFactory.getBeanNamesForType(beanClass, true, false))) {
RootBeanDefinition beanDefinition = new RootBeanDefinition(beanClass);
beanDefinition.setSynthetic(true);
registry.registerBeanDefinition(name, beanDefinition);
}
}
}
}
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(ServerProperties.class)
public class EmbeddedWebServerFactoryCustomizerAutoConfiguration {
@Configuration
//当容器中存在Tomcat相关类就生效
@ConditionalOnClass({ Tomcat.class, UpgradeProtocol.class })
public static class TomcatWebServerFactoryCustomizerConfiguration {
//导入了TomcatWebServerFactoryCustomizer,TomcatWeb服务工厂定制器
@Bean
public TomcatWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer(
Environment environment, ServerProperties serverProperties) {
return new TomcatWebServerFactoryCustomizer(environment, serverProperties);
}
}
@Configuration
@ConditionalOnClass({ Server.class, Loader.class, WebAppContext.class })
public static class JettyWebServerFactoryCustomizerConfiguration {
@Bean
public JettyWebServerFactoryCustomizer jettyWebServerFactoryCustomizer(
Environment environment, ServerProperties serverProperties) {
return new JettyWebServerFactoryCustomizer(environment, serverProperties);
}
}
@Configuration
@ConditionalOnClass({ Undertow.class, SslClientAuthMode.class })
public static class UndertowWebServerFactoryCustomizerConfiguration {
@Bean
public UndertowWebServerFactoryCustomizer undertowWebServerFactoryCustomizer(
Environment environment, ServerProperties serverProperties) {
return new UndertowWebServerFactoryCustomizer(environment, serverProperties);
}
}
@Configuration
@ConditionalOnClass(HttpServer.class)
public static class NettyWebServerFactoryCustomizerConfiguration {
@Bean
public NettyWebServerFactoryCustomizer nettyWebServerFactoryCustomizer(
Environment environment, ServerProperties serverProperties) {
return new NettyWebServerFactoryCustomizer(environment, serverProperties);
}
}
}
这两个类的作用是往Spring容器中导入了一些Web服务工厂定制器(xxxWebServerFactoryCustomizer)。而 ServletWebServerFactoryAutoConfiguration
中使用了 @Import
注解导入了 BeanPostProcessorsRegistrar、EmbeddedTomcat、EmbeddedJetty 和 EmbeddedUndertow
组件,其中后面三个都在 ServletWebServerFactoryConfiguration
类中,根据依赖的Servle容器使其中一个生效。而 BeanPostProcessorsRegistrar 是 ServletWebServerFactoryAutoConfiguration 的内部类,它的作用是往容器中导入了 WebServerFactoryCustomizerBeanPostProcessor
组件。
@Configuration
class ServletWebServerFactoryConfiguration {
@Configuration
//如果有这三个类就生效,即如果依赖了tomcat,这个就生效
@ConditionalOnClass({ Servlet.class, Tomcat.class, UpgradeProtocol.class })
//如果有ServletWebServerFactory就不生效,所以容器中应该只有一个ServletWebServerFactory
@ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
public static class EmbeddedTomcat {
//往容器中导入了TomcatServletWebServerFactory 组件,它继承了ServletWebServerFactory类。
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory() {
return new TomcatServletWebServerFactory();
}
}
@Configuration
@ConditionalOnClass({ Servlet.class, Server.class, Loader.class, WebAppContext.class })
@ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
public static class EmbeddedJetty {
@Bean
public JettyServletWebServerFactory JettyServletWebServerFactory() {
return new JettyServletWebServerFactory();
}
}
@Configuration
@ConditionalOnClass({ Servlet.class, Undertow.class, SslClientAuthMode.class })
@ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
public static class EmbeddedUndertow {
@Bean
public UndertowServletWebServerFactory undertowServletWebServerFactory() {
return new UndertowServletWebServerFactory();
}
}
}
EmbeddedTomcat
往容器中导入了一个 TomcatServletWebServerFactory
组件,它实现了 ServletWebServerFactory
类,也实现了 WebServerFactory
类。WebServerFactoryCustomizerBeanPostProcessor
在 WebServerFactory 初始化时调用上面自动配置类注入的那些Web服务工厂定制器(xxxWebServerFactoryCustomizer)。
public class WebServerFactoryCustomizerBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware {
private List<WebServerFactoryCustomizer<?>> customizers;
//bean初始化前调用
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
//判断这个bean的类型是WebServerFactory
//TomcatServletWebServerFactory继承了WebServerFactory,所以它初始化时,会往下执行
if (bean instanceof WebServerFactory) {
postProcessBeforeInitialization((WebServerFactory) bean);
}
return bean;
}
//bean初始化后调用
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}
@SuppressWarnings("unchecked")
private void postProcessBeforeInitialization(WebServerFactory webServerFactory) {
LambdaSafe
//获取Web服务工厂定制器(WebServerFactoryCustomizer)
.callbacks(WebServerFactoryCustomizer.class, getCustomizers(), webServerFactory)
.withLogger(WebServerFactoryCustomizerBeanPostProcessor.class)
//调用customizer的customize方法,customize方法就是根据相关配置初始化Servlet容器
.invoke((customizer) -> customizer.customize(webServerFactory));
}
private Collection<WebServerFactoryCustomizer<?>> getCustomizers() {
if (this.customizers == null) {
this.customizers = new ArrayList<>(getWebServerFactoryCustomizerBeans());
this.customizers.sort(AnnotationAwareOrderComparator.INSTANCE);
this.customizers = Collections.unmodifiableList(this.customizers);
}
return this.customizers;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private Collection<WebServerFactoryCustomizer<?>> getWebServerFactoryCustomizerBeans() {
//返回WebServerFactoryCustomizer类型的bean(定制器)
//上面自动配置类注册的Web服务工厂定制器(xxxWebServerFactoryCustomizer)
return (Collection) this.beanFactory
.getBeansOfType(WebServerFactoryCustomizer.class, false, false).values();
}
}
所以当Spring容器创建 TomcatServletWebServerFactory 实例时,会调用 WebServerFactoryCustomizerBeanPostProcessor ,然后获取所有的Web服务工厂定制器(xxxWebServerFactoryCustomizer),调用它的 customize 方法,这个方法是根据配置创建servlet容器。
这就是我们在代码实现Servlet容器配置时,需要往容器中注入一个 WebServerFactoryCustomizer,而它的 customize 方法实际上也是设置传入的 WebServerFactory 的属性。
也可以通过手动注入一个 ConfigurableServletWebServerFactory 来实现。ConfigurableServletWebServerFactory 实现了 ServletWebServerFactory,所以自动配置的 TomcatServletWebServerFactory 不生效。而 ConfigurableServletWebServerFactory 又实现了 WebServerFactory,所以它会走 WebServerFactoryCustomizerBeanPostProcessor 的流程,被Web服务工厂定制器配置。
1.3.2 启动原理
步骤:
1、Spring Boot启动运行 run 方法,执行到 SpringApplication 的 run(String… args) 方法。
public class SpringApplication {
public ConfigurableApplicationContext run(String... args) {
...
ConfigurableApplicationContext context = null;
...
//调用createApplicationContext方法,返回AnnotationConfigServletWebServerApplicationContext
context = createApplicationContext();
...
refreshContext(context);
...
return context;
}
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应用,context的类型就是 AnnotationConfigServletWebServerApplicationContext。
2、执行 refreshContext(context) 方法。SpringBoot刷新IOC容器(创建bean对象,并初始化容器,创建容器中的每一个组件)。一路执行,到 refresh(ApplicationContext applicationContext) 方法。
protected void refresh(ApplicationContext applicationContext) {
Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext);
//将applicationContext强转成AbstractApplicationContext类型,然后调用refresh()方法。
((AbstractApplicationContext) applicationContext).refresh();
}
AbstractApplicationContext 的 refresh() 方法。
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
prepareRefresh();
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
prepareBeanFactory(beanFactory);
try {
postProcessBeanFactory(beanFactory);
invokeBeanFactoryPostProcessors(beanFactory);
registerBeanPostProcessors(beanFactory);
initMessageSource();
initApplicationEventMulticaster();
onRefresh();
registerListeners();
finishBeanFactoryInitialization(beanFactory);
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
destroyBeans();
cancelRefresh(ex);
throw ex;
}
finally {
resetCommonCaches();
}
}
}
主要是 onRefresh() 方法。因为 AbstractApplicationContext 是由 applicationContext 强转的,而 applicationContext 是 AnnotationConfigServletWebServerApplicationContext 类型的,所以调用的其实是 AnnotationConfigServletWebServerApplicationContext 的 onRefresh() 方法,而它的 onRefresh() 方法是从它的父类继承过来的,也就是 ServletWebServerApplicationContext 的 onRefresh() 方法。
@Override
protected void onRefresh() {
super.onRefresh();
try {
createWebServer();
}
catch (Throwable ex) {
throw new ApplicationContextException("Unable to start web server", ex);
}
}
这里调用了 createWebServer 方法。
private void createWebServer() {
WebServer webServer = this.webServer;
ServletContext servletContext = getServletContext();
if (webServer == null && servletContext == null) {
//获取ServletWebServerFactory
ServletWebServerFactory factory = getWebServerFactory();
//执行getWebServer方法
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();
}
protected ServletWebServerFactory getWebServerFactory() {
//获取ServletWebServerFactory类型的bean名称
String[] beanNames = getBeanFactory()
.getBeanNamesForType(ServletWebServerFactory.class);
if (beanNames.length == 0) {
throw new ApplicationContextException(
"Unable to start ServletWebServerApplicationContext due to missing "
+ "ServletWebServerFactory bean.");
}
if (beanNames.length > 1) {
throw new ApplicationContextException(
"Unable to start ServletWebServerApplicationContext due to multiple "
+ "ServletWebServerFactory beans : "
+ StringUtils.arrayToCommaDelimitedString(beanNames));
}
return getBeanFactory().getBean(beanNames[0], ServletWebServerFactory.class);
}
因为自动配置类往容器中注入了 TomcatServletWebServerFactory(以tomcat为例,其他Servlet容器相同),它实现了 ServletWebServerFactory,所以最后执行的是 TomcatServletWebServerFactory 的 getWebServer 方法来创建 WebServer:
@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {
//创建了tomcat容器对象
Tomcat tomcat = new Tomcat();
//配置tomcat相关配置
File baseDir = (this.baseDirectory != null) ? this.baseDirectory
: createTempDir("tomcat");
tomcat.setBaseDir(baseDir.getAbsolutePath());
Connector connector = new Connector(this.protocol);
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);
}
protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
return new TomcatWebServer(tomcat, getPort() >= 0);
}
public class TomcatWebServer implements WebServer {
...
public TomcatWebServer(Tomcat tomcat, boolean autoStart) {
Assert.notNull(tomcat, "Tomcat Server must not be null");
this.tomcat = tomcat;
this.autoStart = autoStart;
initialize();
}
private void initialize() throws WebServerException {
logger.info("Tomcat initialized with port(s): " + getPortsDescription(false));
synchronized (this.monitor) {
try {
addInstanceIdToEngineName();
Context context = findContext();
context.addLifecycleListener((event) -> {
if (context.equals(event.getSource())
&& Lifecycle.START_EVENT.equals(event.getType())) {
// Remove service connectors so that protocol binding doesn't
// happen when the service is started.
removeServiceConnectors();
}
});
// Start the server to trigger initialization listeners
//启动tomcat
this.tomcat.start();
// We can re-throw failure exception directly in the main thread
rethrowDeferredStartupExceptions();
try {
ContextBindings.bindClassLoader(context, context.getNamingToken(),
getClass().getClassLoader());
}
catch (NamingException ex) {
}
// Unlike Jetty, all Tomcat threads are daemon threads. We create a
// blocking non-daemon to stop immediate shutdown
startDaemonAwaitThread();
}
catch (Exception ex) {
stopSilently();
throw new WebServerException("Unable to start embedded Tomcat", ex);
}
}
}
}
二、外置Servlet容器
2.1 使用外置Servlet容器
嵌入式的Servlet的容器有个缺点:默认不支持jsp,优化定制比较复杂。所以在适当场景下,我们还是需要外部的servlet容器。
使用外置Servlet容器步骤:
1、创建war类型的maven项目。创建好web项目的目录结构(必须有web.xml文件)。
2、将嵌入式的servlet容器依赖的scope指定为provided。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
3、编写一个 SpringBootServletInitializer
类型的子类,并重写 configure 方法。
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootWebDemo1Application.class);
}
}
4、启动容器。
2.2 外置Servlet容器启动原理
原理对比;
jar 方式:执行SpringBoot主类的main方法,调用 SpringApplication
的 run 方法,启动Spring的ioc容器,创建嵌入式的Servlet容器。
war 方式:启动服务器,服务器启动SpringBoot的 SpringBootServletInitializer
,启动Spring的ioc容器。
Servlet3.0+定义了几个web应用在启动时的规则:
(1)容器在启动时会去每个jar包下找 META-INF/services/javax.servlet.ServletContainerInitializer
文件,如果有则根据这个文件内容创建 ServletContainerInitializer
的实现类实例。
(2)可以使用 @HandlesTypes
注解加载需要的类。
启动原理:
1、启动tomcat容器。
2、容器根据Servlet的规则创建 SpringServletContainerInitializer
。该类在在 spring-web-xxx.jar 下,这个jar包的 META-INF/services/javax.servlet.ServletContainerInitializer 文件内容就是 SpringServletContainerInitializer 的全类名。
@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {
//@HandlesTypes标注的类型的所有类都传入到onStartup方法的参数Set中
@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) {
//每个WebApplicationInitializer调用自己的onStartup方法。
initializer.onStartup(servletContext);
}
}
}
3、SpringBootServletInitializer
实现了 WebApplicationInitializer
接口,所以我们自定义的 SpringBootServletInitializer 的子类(ServletInitializer)会被创建对象,并执行 onStartup
方法。
4、SpringBootServletInitializer 的 onStartup 方法被调用时,调用了 createRootApplicationContext
方法,启动了Spring的ioc容器。
protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {
SpringApplicationBuilder builder = createSpringApplicationBuilder();
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));
}
builder.initializers(
new ServletContextApplicationContextInitializer(servletContext));
builder.contextClass(AnnotationConfigServletWebServerApplicationContext.class);
//子类(ServletInitializer )重写了这个方法,将SpringBoot的主程序类传入进来
builder = configure(builder);
builder.listeners(new WebEnvironmentPropertySourceInitializer(servletContext));
//使用builder创建一个Spring应用
SpringApplication application = builder.build();
if (application.getAllSources().isEmpty() && AnnotationUtils
.findAnnotation(getClass(), Configuration.class) != null) {
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));
}
//启动Spring容器
return run(application);
}
5、SpringBoot应用启动后就创建ioc容器。执行到这一步就和内置Servlet容器启动原理相同了。
一、内置Servlet容器
1.1 定制和修改Servlet容器配置
(1)在配置文件 application.properties/yaml 中修改。示例:
server.port=8081
#项目路径
server.servlet.context-path=/dmf
server.tomcat.uri-encoding=UTF-8
#通用的Servlet容器配置
server.xxx
#Tomcat的设置
server.tomcat.xxx
这些配置对应的是 ServerProperties
,这是个配置类,对应的就是服务的相关配置。
(2)代码里设置。
Spring boot1.x 和 Spring Boot2.x 的配置方法有些不同。
SpringBoot1.x 定制和修改Servlet容器的相关配置示例:
@Configuration
public class MyConfig {
@Bean //必须要加到容器中才能生效
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
//在这里修改相关配置
container.setPort(8088);
}
};
}
}
SpringBoot2.x定制和修改Servlet容器的相关配置示例:
@Configuration
public class MyConfig {
@Bean //必须要加到容器中才能生效
public ConfigurableServletWebServerFactory configurableServletWebServerFactory() {
//修改tomcat相关配置
TomcatServletWebServerFactory factory= new TomcatServletWebServerFactory();
factory.setPort(8083);
return factory;
}
//或者使用下面这种方法,泛型根据具体的servlet容器来写。
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> webServerFactoryCustomizer() {
return new WebServerFactoryCustomizer<TomcatServletWebServerFactory>() {
@Override
public void customize(TomcatServletWebServerFactory factory) {
factory.setPort(8084);
}
};
}
}
上面这两种方法二选一,原理都是一样的。
1.2 切换内置Servlet容器
Spring Boot默认使用tomcat作为Servlet容器,同时也支持Jetty、Undertow等内置Sevlet容器,可以根据需要切换相应的Servlet容器。
步骤:
1、在web-Starter里排除tomcat的依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
2、导入其他的Servlet容器依赖。
<!--引入Jetty-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<!--引入Undertow-->
<dependency>
<artifactId>spring-boot-starter-undertow</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
1.3、内置Servlet容器原理
1.3.1 自动配置原理(2.x版本)
这里以2.x版本中的tomcat为例,找到内置Servlet容器的自动配置类,在spring-boot-autoconfigure.jar中的 ServletWebServerFactoryAutoConfiguration 和 EmbeddedWebServerFactoryCustomizerAutoConfiguration。
@Configuration
@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 {
//往容器中导入了ServletWebServerFactoryCustomizer
//servletWeb服务工程定制器
@Bean
public ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer(
ServerProperties serverProperties) {
return new ServletWebServerFactoryCustomizer(serverProperties);
}
//导入了TomcatServletWebServerFactoryCustomizer
@Bean
@ConditionalOnClass(name = "org.apache.catalina.startup.Tomcat")//当容器中有Tomcat类就生效
public TomcatServletWebServerFactoryCustomizer tomcatServletWebServerFactoryCustomizer(
ServerProperties serverProperties) {
return new TomcatServletWebServerFactoryCustomizer(serverProperties);
}
//这个类的作用是往容器中添加一些组件
public static class BeanPostProcessorsRegistrar
implements ImportBeanDefinitionRegistrar, BeanFactoryAware {
private ConfigurableListableBeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
if (beanFactory instanceof ConfigurableListableBeanFactory) {
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
}
}
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
BeanDefinitionRegistry registry) {
if (this.beanFactory == null) {
return;
}
//往容器中添加了WebServerFactoryCustomizerBeanPostProcessor组件(web服务工厂定制器的后置处理器)。
//这个类实现了BeanPostProcessor,属于bean的后置处理器。作用是在bean初始化前后加一些自己的逻辑处理
registerSyntheticBeanIfMissing(registry,
"webServerFactoryCustomizerBeanPostProcessor",
WebServerFactoryCustomizerBeanPostProcessor.class);
registerSyntheticBeanIfMissing(registry,
"errorPageRegistrarBeanPostProcessor",
ErrorPageRegistrarBeanPostProcessor.class);
}
private void registerSyntheticBeanIfMissing(BeanDefinitionRegistry registry,
String name, Class<?> beanClass) {
if (ObjectUtils.isEmpty(
this.beanFactory.getBeanNamesForType(beanClass, true, false))) {
RootBeanDefinition beanDefinition = new RootBeanDefinition(beanClass);
beanDefinition.setSynthetic(true);
registry.registerBeanDefinition(name, beanDefinition);
}
}
}
}
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(ServerProperties.class)
public class EmbeddedWebServerFactoryCustomizerAutoConfiguration {
@Configuration
//当容器中存在Tomcat相关类就生效
@ConditionalOnClass({ Tomcat.class, UpgradeProtocol.class })
public static class TomcatWebServerFactoryCustomizerConfiguration {
//导入了TomcatWebServerFactoryCustomizer,TomcatWeb服务工厂定制器
@Bean
public TomcatWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer(
Environment environment, ServerProperties serverProperties) {
return new TomcatWebServerFactoryCustomizer(environment, serverProperties);
}
}
@Configuration
@ConditionalOnClass({ Server.class, Loader.class, WebAppContext.class })
public static class JettyWebServerFactoryCustomizerConfiguration {
@Bean
public JettyWebServerFactoryCustomizer jettyWebServerFactoryCustomizer(
Environment environment, ServerProperties serverProperties) {
return new JettyWebServerFactoryCustomizer(environment, serverProperties);
}
}
@Configuration
@ConditionalOnClass({ Undertow.class, SslClientAuthMode.class })
public static class UndertowWebServerFactoryCustomizerConfiguration {
@Bean
public UndertowWebServerFactoryCustomizer undertowWebServerFactoryCustomizer(
Environment environment, ServerProperties serverProperties) {
return new UndertowWebServerFactoryCustomizer(environment, serverProperties);
}
}
@Configuration
@ConditionalOnClass(HttpServer.class)
public static class NettyWebServerFactoryCustomizerConfiguration {
@Bean
public NettyWebServerFactoryCustomizer nettyWebServerFactoryCustomizer(
Environment environment, ServerProperties serverProperties) {
return new NettyWebServerFactoryCustomizer(environment, serverProperties);
}
}
}