Spring Boot如何自动包含在pom文件中包含的依赖项中的代码?

huangapple go评论60阅读模式
英文:

How does Spring Boot automatically include code from dependencies included in the pom file?

问题

例如,当我们在项目中包含spring-boot-starter-web时,它知道如何设置并运行Tomcat,而无需任何外部提示。Spring是如何知道如何做到这一点的?

我有一些常见的组件,我想在多个项目中包含它们,并让它们以类似的方式启动。

我不知道从哪里开始。

英文:

For example, when we include spring-boot-starter-web in a project it knows to set up and run tomcat without any external prompting. How does spring know how to do that?

I have some common components that I would like to include in multiple projects, and have them start in a similar fashion.

I have no idea where to start.

答案1

得分: 2

Spring Boot利用了"starters"的概念,即具有自动配置类的依赖项。这些类通常带有@ConditionalOn*注解,描述了bean,当应用程序上下文启动时,Spring会扫描这些类并决定是否可以实例化bean的自动配置。在您的情况下,有ServletWebServerFactoryAutoConfiguration,它又导入了ServletWebServerFactoryConfiguration.EmbeddedTomcat:

这里有@ConditionalOnClass({ Servlet.class, Tomcat.class, UpgradeProtocol.class }),这意味着当这些类在类路径中存在时,Tomcat应该被启动。

要深入了解"starter"的概念,请查看https://www.baeldung.com/spring-boot-starters。

英文:

Spring Boot leverages the concept of starters, e.i. dependencies with autoconfiguration classes. Those classed usually have @ConditionalOn* annotation with described bean and when application context is starting up Spring scans those classes and decides whether autoconfiguration eligible for bean instantiation. In your case there's ServletWebServerFactoryAutoConfiguration which in turn imports ServletWebServerFactoryConfiguration.EmbeddedTomcat:

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

	@Bean
	TomcatServletWebServerFactory tomcatServletWebServerFactory(
			ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers,
			ObjectProvider<TomcatContextCustomizer> contextCustomizers,
			ObjectProvider<TomcatProtocolHandlerCustomizer<?>> protocolHandlerCustomizers) {
		TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
		factory.getTomcatConnectorCustomizers()
				.addAll(connectorCustomizers.orderedStream().collect(Collectors.toList()));
		factory.getTomcatContextCustomizers()
				.addAll(contextCustomizers.orderedStream().collect(Collectors.toList()));
		factory.getTomcatProtocolHandlerCustomizers()
				.addAll(protocolHandlerCustomizers.orderedStream().collect(Collectors.toList()));
		return factory;
	}
}

Here we have @ConditionalOnClass({ Servlet.class, Tomcat.class, UpgradeProtocol.class }) which means that Tomcat should be started when those classes are present in the classpath.

For deeper understanding of starter concept see https://www.baeldung.com/spring-boot-starters

huangapple
  • 本文由 发表于 2023年3月9日 21:45:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定