SpringBoot如何实现Tomcat自动配置

 更新时间:2021年03月12日 09:30:40   作者:筱进  
这篇文章主要介绍了SpringBoot如何实现Tomcat自动配置,帮助大家更好的理解和学习使用SpringBoot框架,感兴趣的朋友可以了解下

目录

准备工作

我们知道SpringBoot的自动装配的秘密在 org.springframework.boot.autoconfigure 包下的 spring.factories 文件中,而嵌入Tomcat的原理就在这个文件中加载的一个配置类: org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration

@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 {

	@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);
	}

	/**
	 * Registers a {@link WebServerFactoryCustomizerBeanPostProcessor}. Registered via
	 * {@link ImportBeanDefinitionRegistrar} for early registration.
	 */
	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;
			}
			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);
			}
		}

	}

}

首先看一下上方的几个注解

  • @AutoConfigureOrder 这个注解是决定配置类的加载顺序的,当注解里的值越小越先加载,而 Ordered.HIGHEST_PRECEDENCE 的值是 Integer.MIN_VALUE 也就是说这个类肯定是最先加载的那一批
  • @ConditionalOnXXX 在之前的文章中已经无数次提到了,就不再阐述了
  • @EnableConfigurationProperties 开启 ServerProperties 类的属性值配置。而这个类里面包含的就是Web服务的配置
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {

	private Integer port;

	private InetAddress address;

	@NestedConfigurationProperty
	private final ErrorProperties error = new ErrorProperties();

	private Boolean useForwardHeaders;

	private String serverHeader;

	private int maxHttpHeaderSize = 0; // bytes

	private Duration connectionTimeout;

	@NestedConfigurationProperty
	private Ssl ssl;

	@NestedConfigurationProperty
	private final Compression compression = new Compression();

	@NestedConfigurationProperty
	private final Http2 http2 = new Http2();

	private final Servlet servlet = new Servlet();

	private final Tomcat tomcat = new Tomcat();

	private final Jetty jetty = new Jetty();

	private final Undertow undertow = new Undertow();
}

这个类的代码太多了,这里就不一一贴出来了,我们平常在 application.properties 中配置的server.xxx就是这个类中属性

@Import
BeanPostProcessorsRegistrar
public static class BeanPostProcessorsRegistrar
			implements ImportBeanDefinitionRegistrar, BeanFactoryAware {
		@Override
		public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
				BeanDefinitionRegistry registry) {
			if (this.beanFactory == null) {
				return;
			}
			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);
			}
		}

	}

这个类注册了两个bean: WebServerFactoryCustomizerBeanPostProcessorErrorPageRegistrarBeanPostProcessor 关于这两个bean的作用稍后再详细介绍

  • EmbeddedTomcat
@Configuration
@ConditionalOnClass({ Servlet.class, Tomcat.class, UpgradeProtocol.class })
@ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
public static class EmbeddedTomcat {

 @Bean
 public TomcatServletWebServerFactory tomcatServletWebServerFactory() {
  return new TomcatServletWebServerFactory();
 }

}

这个类会在存在Tomcat相关jar包时添加一个 TomcatServletWebServerFactory bean

其他两个相信大家都知道怎么回事了

除了这些这个类还注入了两个类 ServletWebServerFactoryCustomizerTomcatServletWebServerFactoryCustomizer
现在前期准备工作已经做好了,看一下这个Tomcat是如何启动的吧

启动

启动入口在 ServletWebServerApplicationContext 中的 onRefresh 方法

protected void onRefresh() {
	super.onRefresh();
 try {
 	createWebServer();
 }
	catch (Throwable ex) {
		throw new ApplicationContextException("Unable to start web server", ex);
	}
}

Tomcat的启动就在 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();
}

首先看一下 getWebServerFactory

protected ServletWebServerFactory getWebServerFactory() {
 // 这里获取的beanname就是上方注册的tomcatServletWebServerFactory了
 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);
}

准备环境里注册的bean现在出来一个了。注意,上方还注册了一个后置处理器 EmbeddedServletContainerCustomizerBeanPostProcessor ,获取bean tomcatServletWebServerFactory 的时候就会执行后置处理器的 postProcessBeforeInitialization 方法

public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
	if (bean instanceof WebServerFactory) {
		postProcessBeforeInitialization((WebServerFactory) bean);
	}
	return bean;
}
private void postProcessBeforeInitialization(WebServerFactory webServerFactory) {
 LambdaSafe
  .callbacks(WebServerFactoryCustomizer.class, getCustomizers(),
     webServerFactory)
  .withLogger(WebServerFactoryCustomizerBeanPostProcessor.class)
  .invoke((customizer) -> customizer.customize(webServerFactory));
}

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;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private Collection<WebServerFactoryCustomizer<?>> getWebServerFactoryCustomizerBeans() {
 return (Collection) this.beanFactory
  .getBeansOfType(WebServerFactoryCustomizer.class, false, false).values();
}

这个处理器的作用是获得所有定制器,然后执行定制器的方法

接着往下看

这个时候就可以启动Tomcat了

public WebServer getWebServer(ServletContextInitializer... initializers) {
 Tomcat tomcat = new 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 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 {
 TomcatWebServer.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
   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) {
    // Naming is not enabled. Continue
   }

   // Unlike Jetty, all Tomcat threads are daemon threads. We create a
   // blocking non-daemon to stop immediate shutdown
   startDaemonAwaitThread();
  }
  catch (Exception ex) {
   throw new WebServerException("Unable to start embedded Tomcat", ex);
  }
 }
}

以上就是SpringBoot如何实现Tomcat自动配置的详细内容,更多关于SpringBoot实现Tomcat自动配置的资料请关注脚本之家其它相关文章!

相关文章

  • Java运算符的知识点与代码汇总

    Java运算符的知识点与代码汇总

    这篇文章主要给大家总结介绍了关于Java运算符知识点与代码的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • SpringBoot AnnotationUtils工具类的使用实例详解

    SpringBoot AnnotationUtils工具类的使用实例详解

    这篇文章主要介绍了SpringBoot AnnotationUtils工具类的使用,使用自定义注解标记业务方法,原生Java获取注解及AnnotationUtils工具类获取方法,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-09-09
  • SpringBoot中实现异步调用@Async详解

    SpringBoot中实现异步调用@Async详解

    这篇文章主要介绍了SpringBoot中实现异步调用@Async详解,在SpringBoot的日常开发中,一般都是同步调用的,但实际中有很多场景非常适合使用异步来处理,需要的朋友可以参考下
    2024-01-01
  • Java 中的伪共享详解及解决方案

    Java 中的伪共享详解及解决方案

    这篇文章主要介绍了Java 中的伪共享详解及解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • Springboot实现WebMvcConfigurer接口定制mvc配置详解

    Springboot实现WebMvcConfigurer接口定制mvc配置详解

    这篇文章主要介绍了Springboot实现WebMvcConfigurer接口定制mvc配置详解,spring boot抛弃了传统xml配置文件,通过配置类(标注@Configuration的类,@Configuration配置类相当于一个xml配置文件)以JavaBean形式进行相关配置,需要的朋友可以参考下
    2023-09-09
  • struts升级到2.5.2遇到的问题及解决方案(推荐)

    struts升级到2.5.2遇到的问题及解决方案(推荐)

    原来的版本是2.3.x,由于安全原因需要升级到2.5.2。但是在升级过程中遇到各种各样的问题,下面小编给大家带来了struts升级到2.5.2遇到的问题及解决方案,需要的朋友参考下吧
    2016-11-11
  • 关于java自定义线程池的原理与实现

    关于java自定义线程池的原理与实现

    本文介绍了如何自定义线程池和阻塞队列,包括阻塞队列的实现方法,线程池的构建以及拒绝策略的应用,详细阐述了线程池中任务的提交和执行流程,以及如何处理任务超出队列容量的情况
    2022-04-04
  • Springboot+MyBatist实现前后台交互登陆功能方式

    Springboot+MyBatist实现前后台交互登陆功能方式

    这篇文章主要介绍了Springboot+MyBatist实现前后台交互登陆功能方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • javaweb实现投票系统

    javaweb实现投票系统

    这篇文章主要为大家详细介绍了javaweb实现投票系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-09-09
  • SpringBoot feign动态设置数据源(https请求)

    SpringBoot feign动态设置数据源(https请求)

    这篇文章主要介绍了SpringBoot如何在运行时feign动态添加数据源,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2021-08-08

最新评论