org.springframework.boot.spring-boot POM的目的是什么?

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

What is the purpose of org.springframework.boot.spring-boot POM?

问题

这个POM在Spring Boot org.springframework.boot.spring-boot中的目的是什么?

它包括了:

org.springframework.spring-core
org.springframework.spring-context

因此在类似于org.springframework.boot.spring-boot-starter-web的项目中,你会有:

org.springframework.spring-webmvc
org.springframework.boot.spring-boot-starter
org.springframework.boot.spring-boot-starter-json
org.springframework.boot.spring-boot-starter-tomcat

为什么不直接将org.springframework.boot.spring-boot-starter-web设计为:

org.springframework.spring-webmvc
org.springframework.spring-core
org.springframework.spring-context    
org.springframework.boot.spring-boot-starter-json
org.springframework.boot.spring-boot-starter-tomcat
英文:

What is the purpose of this POM in Sprint Boot org.springframework.boot.spring-boot

It includes

org.springframework.spring-core
org.springframework.spring-context

So in something like org.springframework.boot.spring-boot-starter-web you have

org.springframework.spring-webmvc
org.springframework.boot.spring-boot-starter
org.springframework.boot.spring-boot-starter-json
org.springframework.boot.spring-boot-starter-tomcat

Why not just make org.springframework.boot.spring-boot-starter-web

org.springframework.spring-webmvc
org.springframework.spring-core
org.springframework.spring-context    
org.springframework.boot.spring-boot-starter-json
org.springframework.boot.spring-boot-starter-tomcat

答案1

得分: 2

spring-boot-starter有以下依赖项:

  • jakarta.annotation-api
  • spring-core
  • spring-boot
  • spring-boot-autoconfigure
  • spring-boot-starter-logging

如果将spring-boot-starter替换为spring-corespring-context,那么您将错过在spring-bootspring-boot-autoconfigure中定义的所有类和注解(spring-boot依赖于spring-corespring-context,但它自己实现了一些重要的类,见下文)。

spring-boot-starter-web为例,通常我们需要在application.yml中设置server.port,这个属性在spring-boot-autoconfigure项目的ServerProperties类中定义(这就是为什么我们需要在org.springframework.boot.spring-boot-starter-web中使用spring-boot-starter),还有其他常用的属性,如server.addressserver.servlet.context-pathserver.ssl.*等等。它们都在spring-boot-autoconfigure项目中定义。

为了自动配置在ServerProperties中定义的属性,创建了ServletWebServerFactoryAutoConfiguration类,将ServerProperties作为参数,并将其值应用于设置类似tomcat的软件。

现在看看ServerProperties既没有使用@Configuration也没有使用@Component进行注解,因此在进行组件扫描时不会实例化它,因此创建了EnableConfigurationProperties注解来确保ServerProperties的实例将被注入。这个注解在spring boot项目中定义,所以我们需要spring-boot依赖。它在这里被使用。

关于什么是spring boot starter以及它是如何工作的,这篇文章很有帮助:快速构建Spring Boot Starter的简易指南

英文:

spring-boot-starter has following dependencies:

jakarta.annotation-api
spring-core
spring-boot
spring-boot-autoconfigure
spring-boot-starter-logging

if you replace spring-boot-starter with spring-core and spring-context, then you miss out all the classes and annotations that are defined in spring-boot and spring-boot-autoconfigure(spring-boot depends on spring-core and spring-context, but it has implemented some important classes itself, see below).

take spring-boot-starter-web as an example, usually we need to set server.port in our application.yml, this property is defined in ServerProperties class in spring-boot-autoconfigure project(that's why we need spring-boot-starter in org.springframework.boot.spring-boot-starter-web), there are other commonly used properties such as server.address, server.servlet.context-path, and server.ssl.* ....etc.They are all defined here in spring-boot-autoconfigure project.

in order to auto configure the properties defined in ServerProperties, class ServletWebServerFactoryAutoConfiguration is created to take ServerProperties as a parameter and apply it's values to set up software like tomcat

now see that ServerProperties is neither annotated with @Configuration nor @Component, so it wouldn't be instantiated when component-scan is happening, so this annotation EnableConfigurationProperties is created to make sure ServerProperties's instance will be injected. this annotation is defined in spring boot project, that's why we need spring-boot dependency. And it's used
here.

for what is a spring boot starter and how it works, here is a helpful article: Quick Guide to Building a Spring Boot Starter

huangapple
  • 本文由 发表于 2020年9月3日 07:14:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63714674.html
匿名

发表评论

匿名网友

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

确定