简体   繁体   English

如何创建 Spring 5 组件索引?

[英]How can I create a Spring 5 component index?

Spring Framework 5 apparently contains support for a "component index" which lives in META-INF/spring.components and can be used to avoid the need for class-path scanning, and thus, I assume, improve a webapps' startup time. Spring Framework 5 显然包含对位于 META-INF/spring.components 中的“组件索引”的支持,可用于避免类路径扫描的需要,因此,我认为,可以改善 web 应用程序的启动时间。

See:看:

How can I create such a component index for an existing web app I plan to upgrade to Spring 5?如何为我计划升级到 Spring 5 的现有 Web 应用程序创建这样的组件索引?

(Ideally it would get generated automatically at build time with Maven I imagine, but any other workable approaches would at least give me a starting point to work from) (理想情况下,它会在我想象的 Maven 构建时自动生成,但任何其他可行的方法至少会给我一个工作起点)

Spring 5 Has added a new feature to improve startup performance of large applications. Spring 5增加了一个新特性来提高大型应用程序的启动性能。

it creates a list of component candidates at compilation time.它在编译时创建一个候选组件列表。

In this mode, all modules of the application must use this mechanism as, when the ApplicationContext detects such index, it will automatically use it rather than scanning the classpath.在这种模式下,应用程序的所有模块都必须使用这种机制,因为当 ApplicationContext 检测到此类索引时,它会自动使用它而不是扫描类路径。

To generate the index, we just need to add below dependency to each module要生成索引,我们只需要为每个模块添加以下依赖项

Maven:马文:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-indexer</artifactId>
        <version>5.0.3.RELEASE</version>
        <optional>true</optional>
    </dependency>
</dependencies>

Gradle摇篮

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

This process will generate a META-INF/spring.components file that is going to be included in the jar.此过程将生成一个META-INF/spring.components文件,该文件将包含在 jar 中。

Reference : 1.10.9.参考: 1.10.9。 Generating an index of candidate components 生成候选组件的索引

The META-INF/spring.components files are generated by an annotation processor library called spring-context-indexer . META-INF/spring.components文件由名为spring-context-indexer的注释处理器库生成。 If you add this library as "annotation processor path" to the maven-compiler-plugin, the files will be generated automatically at build time:如果将此库作为“注释处理器路径”添加到 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>

This setup requires maven-compiler-plugin version 3.5 or greater.此设置需要 maven-compiler-plugin 3.5 或更高版本。

See also: https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#annotationProcessorPaths另见: https : //maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#annotationProcessorPaths

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在 Spring 中为 @Component 类创建一个构造函数吗 - Can I create a constructor for a @Component class in Spring 如何在Swing中创建此组件? - How can I create this component in Swing? 如何在Java中创建索引文件? - How can I create an index file in java? 如何在没有Spring Boot的情况下创建Spring Sleuth应用程序 - How can I create a Spring sleuth application without Spring boot 如何禁用在 Spring 中使用 @Component 注释创建 bean? - How can I disable creating bean with @Component annotation in Spring? 如何使 Java Spring 组件 class 线程安全? - How can I make a Java Spring Component class thread safe? 是否可以为多模块项目创建 1 个 Spring 组件索引? - Is it possible to create 1 Spring component index for a multi-module project? 在wicket中,如何创建未由组件层次结构/布局定义的RadioGroup? - In wicket, how can I create RadioGroups that are not defined by component hierarchy/layout? 如何使用 BorderLayout 创建一个框架并为每个空间分配一个组件? - How can I create a frame with a BorderLayout and assign each space a component? 如何创建一个起始索引为1(而不是0)的ArrayList - How can I create an ArrayList with a starting index of 1 (instead of 0)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM