iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot如何实现Tomcat自动配置
  • 728
分享到

SpringBoot如何实现Tomcat自动配置

2024-04-02 19:04:59 728人浏览 泡泡鱼

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

摘要

目录准备工作 启动 接着往下看 目录 准备工作 我们知道SpringBoot的自动装配的秘密在 org.springframework.boot.autoconfigure 包下的

目录

准备工作

我们知道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);
	}

	
	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自动配置的资料请关注编程网其它相关文章!

--结束END--

本文标题: SpringBoot如何实现Tomcat自动配置

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

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

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringBoot如何实现Tomcat自动配置
    目录准备工作 启动 接着往下看 目录 准备工作 我们知道SpringBoot的自动装配的秘密在 org.springframework.boot.autoconfigure 包下的...
    99+
    2022-11-11
  • SpringBoot自动配置如何实现
    这篇文章主要介绍“SpringBoot自动配置如何实现”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“SpringBoot自动配置如何实现”文章能帮助大家解决问题。springboot如何实现在之前的...
    99+
    2023-06-30
  • springboot中如何利用Tomcat容器实现自启动
    这篇“springboot中如何利用Tomcat容器实现自启动”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“springbo...
    99+
    2023-06-08
  • springboot自动刷新配置怎么实现
    在Spring Boot中,可以通过使用Spring Cloud Config或Spring Boot Actuator来实现自动刷...
    99+
    2023-10-10
    springboot
  • SpringBoot自动配置Quartz的实现步骤
    目录1. 依赖信息 1.1 原依赖信息 1.2 新的依赖 1.3 依赖变化 2. 新的依赖使用 2.1 默认配置可用 2.2 使用自动配置 2.3 配置信息: 3. 总结 Sprin...
    99+
    2022-11-12
  • 使用springboot怎么实现自动配置
    这篇文章将为大家详细讲解有关使用springboot怎么实现自动配置,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。springboot是什么springboot一种全新的编程规范,其设计目的...
    99+
    2023-06-14
  • Springboot 如何关闭自动配置
    目录Springboot 关闭自动配置1. 关闭Redis自动配置2. SpringBoot默认会自动配置数据库开启关闭自动任务配置流程1.需求2.解决方案Springboot 关闭...
    99+
    2022-11-12
  • SpringBoot是如何实现自动配置的你知道吗
    目录SpringBoot启动类:步骤如下: 总结SpringBoot启动类: @SpringBootApplication public class CommunityAppli...
    99+
    2022-11-12
  • SpringBoot自动配置实现的详细步骤
    目录springboot如何实现一、依赖管理特性1. 父项目2. 场景启动器二、自动配置1. 自动配置组件2. 默认的包结构3. 各种配置拥有默认值4. 按需加载所有自动配置项三、小...
    99+
    2022-11-13
  • SpringBoot开发实战之自动配置
    在介绍SpringBoot的自动配置之前,先了解下注解@Import的使用,SpringBoot的@Enable*开头的注解底层依赖于@Import注解导入一些类,使用@Import...
    99+
    2022-11-12
  • Springboot自动配置与@Configuration配置类实例分析
    这篇文章主要介绍“Springboot自动配置与@Configuration配置类实例分析”,在日常操作中,相信很多人在Springboot自动配置与@Configuration配置类实例分析问题上存在疑惑,小编查阅了各式资料,整理出简单好...
    99+
    2023-07-02
  • SpringBoot自定义Starter与自动配置实现方法详解
    目录前言本次练习的代码仓库代码简要说明custom-springboot-starter-demo 的pom文件customer-starter 的pom文件test 的pom文件配...
    99+
    2023-02-07
    SpringBoot自定义Starter SpringBoot自动配置
  • SpringBoot中@ConfigurationProperties实现配置自动绑定的方法
    目录代码构造器绑定结合@PropertySource代码 pom.xml: <xml version="1.0" encoding="UTF-8"> <projec...
    99+
    2022-11-13
  • 全面解析SpringBoot自动配置的实现原理
    之前一直在用SpringBoot框架,一直感觉SpringBoot框架自动配置的功能很强大,但是并没有明白它是怎么实现自动配置的,现在有空研究了一下,大概明白了SpringBoot框架是怎么实现自动配置的功能,我们编写一个最简单的自动配置功...
    99+
    2023-05-31
    spring boot 配置
  • SpringBoot自动配置Quartz的实现步骤是什么
    本篇内容介绍了“SpringBoot自动配置Quartz的实现步骤是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!1. 依赖信息spri...
    99+
    2023-06-25
  • Java tomcat如何手动配置servlet
    小编给大家分享一下Java tomcat如何手动配置servlet,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!1.准备工作如下图,先按照要求创建好各个...
    99+
    2023-06-21
  • linux如何设置tomcat自启动
    这篇文章主要介绍“linux如何设置tomcat自启动”,在日常操作中,相信很多人在linux如何设置tomcat自启动问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”linux如何设置tomcat自启动”的疑...
    99+
    2023-07-05
  • 浅析SpringBoot自动装配的实现
    目录背景解析起始具体解析结论备注背景 众所周知,如下即可启动一个最简单的Spring应用。查看@SpringBootApplication注解的源码,发现这个注解上有一个重要的注解@...
    99+
    2022-11-13
  • SpringBoot DataSource数据源实现自动配置流程详解
    目录一、重点概念1、什么是DataSource数据源2、数据库连接池二、导入依赖三、分析自动配置1、DataSourceAutoConfiguration类2、DataSourceT...
    99+
    2022-11-13
  • SpringBoot通过自定义注解实现配置类的自动注入的实现
    目录前言实现思路自定义配置类读取配置自定义注解创建子配置Bean通过反射进行people bean的注入使用效果回顾延伸前言 SpringBoot中通过@ConfigurationP...
    99+
    2023-05-16
    SpringBoot配置类自动注入 SpringBoot 自动注入
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作