如何创建Spring 5组件索引?

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

看到:

  • “spring有什么新东西”提到
  • 制定支持的jira问题
  • spring.components格式的一些示例似乎来自实现它的更改

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

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

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

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

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

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

Maven的:

   org.springframework spring-context-indexer 5.0.3.RELEASE true   

摇篮

 dependencies { compileOnly("org.springframework:spring-context-indexer:5.0.3.RELEASE") } 

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

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

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

org.apache.maven.plugins maven-compiler-plugin org.springframework spring-context-indexer 5.0.6.RELEASE ...

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

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