Spring Web如何需要Apache HttpComponents(HTTPClient)而不将其声明为依赖项?

idfiyjo8  于 2023-08-02  发布在  Spring
关注(0)|答案(1)|浏览(113)

HttpComponentsClientHttpRequestFactory的Spring(Spring Web)文档中,
注意:自Spring 6.0起,需要Apache HttpComponents 5.1或更高版本.
当我查看HttpComponentsClientHttpRequestFactory.java的代码时,我还可以清楚地看到相应的导入和对Apache HttpComponents HTTPClient的引用。
然而,当我查看Spring Web的(maven)依赖关系declaration(或其POM)时,我没有看到Apache HttpComponents HTTPClient被声明为依赖关系。
在此添加更多的上下文(对于那些可能会错过注解部分的人,我在注解部分对此进行了说明),Apache HttpComponents HTTPClient库是否应该至少声明为Spring Web的可选依赖项(<optional>true</optional>)?为什么(或如何)它甚至没有在其POM中声明(更不用说标记为可选)?
这是如何工作的?是否有人知道并可以帮助分享这些建议?
例如,下面是旧版本org.springframework.boot:spring-boot-starter-web:jar:2.7.0的依赖关系树

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------< dcm.dev.util:maven-dependency-resolver-tool >-------------
[INFO] Building maven-dependency-resolver-tool 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:3.2.0:tree (default-cli) @ maven-dependency-resolver-tool ---
[INFO] dcm.dev.util:maven-dependency-resolver-tool:jar:1.0
[INFO] \- org.springframework.boot:spring-boot-starter-web:jar:3.1.1:compile
[INFO]    +- org.springframework.boot:spring-boot-starter:jar:3.1.1:compile
[INFO]    |  +- org.springframework.boot:spring-boot:jar:3.1.1:compile
[INFO]    |  +- org.springframework.boot:spring-boot-autoconfigure:jar:3.1.1:compile
[INFO]    |  +- org.springframework.boot:spring-boot-starter-logging:jar:3.1.1:compile
[INFO]    |  |  +- ch.qos.logback:logback-classic:jar:1.4.8:compile
[INFO]    |  |  |  +- ch.qos.logback:logback-core:jar:1.4.8:compile
[INFO]    |  |  |  \- org.slf4j:slf4j-api:jar:2.0.7:compile
[INFO]    |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.20.0:compile
[INFO]    |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.20.0:compile
[INFO]    |  |  \- org.slf4j:jul-to-slf4j:jar:2.0.7:compile
[INFO]    |  +- jakarta.annotation:jakarta.annotation-api:jar:2.1.1:compile
[INFO]    |  +- org.springframework:spring-core:jar:6.0.10:compile
[INFO]    |  |  \- org.springframework:spring-jcl:jar:6.0.10:compile
[INFO]    |  \- org.yaml:snakeyaml:jar:1.33:compile
[INFO]    +- org.springframework.boot:spring-boot-starter-json:jar:3.1.1:compile
[INFO]    |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.15.2:compile
[INFO]    |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.15.2:compile
[INFO]    |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.15.2:compile
[INFO]    |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.15.2:compile
[INFO]    |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.15.2:compile
[INFO]    |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.15.2:compile
[INFO]    +- org.springframework.boot:spring-boot-starter-tomcat:jar:3.1.1:compile
[INFO]    |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:10.1.10:compile
[INFO]    |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:10.1.10:compile
[INFO]    |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:10.1.10:compile
[INFO]    +- org.springframework:spring-web:jar:6.0.10:compile
[INFO]    |  +- org.springframework:spring-beans:jar:6.0.10:compile
[INFO]    |  \- io.micrometer:micrometer-observation:jar:1.10.8:compile
[INFO]    |     \- io.micrometer:micrometer-commons:jar:1.10.8:compile
[INFO]    \- org.springframework:spring-webmvc:jar:6.0.10:compile
[INFO]       +- org.springframework:spring-aop:jar:6.0.10:compile
[INFO]       +- org.springframework:spring-context:jar:6.0.10:compile
[INFO]       \- org.springframework:spring-expression:jar:6.0.10:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

字符串

更新(2023年7月21日):多亏了@Slaw,我意识到Spring在2.3版本(2020年6月)中切换到了Gradle,这或许可以解释构建是如何工作的(因为它 * 在Gradle构建中 * 被声明为可选的依赖项)。

那么org.springframework.build.optional-dependencies插件在这方面有什么作用吗?
谢了,谢了

bxjv4tth

bxjv4tth1#

在Spring框架中,一些组件可能具有可选的依赖项,这些依赖项没有在项目的POM(项目对象模型)文件中显式声明。相反,这些依赖项在运行时基于某些条件有条件地加载。这种机制是通过Spring使用条件依赖和动态类加载来实现的。
当您看到HttpComponentsClientHttpRequestFactory的文档说明它需要Apache HttpComponents 5.1或更高版本,并且您可以在代码中看到对Apache HttpComponents HTTPClient的相应导入和引用时,这意味着该类使用了Apache HttpComponents的特性。
但是,Apache HttpComponents在项目的POM中没有显式地声明为spring-boot-starter-web的直接依赖项。相反,它可能是可选的或可传递的依赖项。
在提供的依赖关系树中,可以看到spring-webspring-webmvcspring-boot-starter-web工件的直接依赖关系。但是,如果仔细观察,您会发现spring-web对org.apache.httpcomponents:httpclient有一个传递依赖。
因此,Apache HttpComponents HTTPClient作为依赖项的实际声明隐藏在spring-web的传递依赖项中,这是spring-boot-starter-web的直接依赖项。
这种方法允许Spring维护一个更加轻量级和灵活的框架,因为用户可以通过相应地管理依赖关系来选择包含或排除特定的功能。它还避免了与其他库的版本冲突,这些库可能对Apache HttpComponents版本有不同的要求。

相关问题