如何创建Spring 5组件索引?

Mat*_*ard 16 java spring spring-mvc maven

Spring Framework 5显然包含对"组件索引"的支持,它存在于META-INF/spring.components中,可用于避免类路径扫描的需要,因此,我认为,可以改善webapps的启动时间.

看到:

如何为我计划升级到Spring 5的现有Web应用程序创建这样的组件索引?

(理想情况下,它会在我想象的Maven构建时自动生成,但任何其他可行的方法至少会给我一个工作的起点)

Nir*_*ane 10

Spring 5添加了一项新功能,以提高大型应用程序的启动性能.

它在编译时创建一个候选组件列表.

在此模式下,应用程序的所有模块都必须使用此机制,因为当ApplicationContext检测到此类索引时,它将自动使用它而不是扫描类路径.

要生成索引, 我们只需要为每个模块添加以下依赖项

Maven的:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-indexer</artifactId>
        <version>5.0.3.RELEASE</version>
        <optional>true</optional>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

摇篮

dependencies {
    compileOnly("org.springframework:spring-context-indexer:5.0.3.RELEASE")
}
Run Code Online (Sandbox Code Playgroud)

此过程将生成将包含在jar中的META-INF/spring.components文件.

参考:1.10.9.生成候选组件的索引

  • 如果您阅读文档,它说它在 IDE 中需要 **annotation-processor**: *在 IDE 中使用此模式时,必须将 spring-context-indexer 注册为注释处理器以确保索引已启动到目前为止,候选组件已更新。*!我认为它没有与 maven 集成 :-( (2认同)

Ste*_*stl 5

这些META-INF/spring.components文件由称为的注释处理器库生成spring-context-indexer。如果将此库作为“注释处理器路径”添加到maven-compiler-plugin,则文件将在构建时自动生成:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <annotationProcessorPaths>
      <path>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-indexer</artifactId>
        <version>5.0.6.RELEASE</version>
      </path>
    </annotationProcessorPaths>
    ...
  </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

此设置需要maven-compiler-plugin 3.5或更高版本。

另请参阅:https : //maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#annotationProcessorPaths