Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

See:

  • The "what's new in spring 5" mention
  • The jira issue under which the support was developed
  • Some examples of what the spring.components format seems to be from the change implementing it

How can I create such a component index for an existing web app I plan to upgrade to Spring 5?

(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)

like image 431
Matt Sheppard Avatar asked Nov 12 '17 22:11

Matt Sheppard


People also ask

How do you create a Spring component?

Go to the src > main > java > your package name > right-click > New > Java Class and create your component class and mark it with @Component annotation.

What is the use of @component in Spring?

@Component is an annotation that allows Spring to automatically detect our custom beans. In other words, without having to write any explicit code, Spring will: Scan our application for classes annotated with @Component. Instantiate them and inject any specified dependencies into them.

Why is Spring 5?

Spring 5's most exciting new feature is its reactive programming model. The Spring 5 framework is built on a reactive foundation and is fully asynchronous and nonblocking. The new event-loop execution model can scale vertically with only a small number of threads.


2 Answers

Spring 5 Has added a new feature to improve startup performance of large applications.

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.

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.

Reference : 1.10.9. Generating an index of candidate components

like image 178
Niraj Sonawane Avatar answered Sep 24 '22 11:09

Niraj Sonawane


The META-INF/spring.components files are generated by an annotation processor library called 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:

<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.

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

like image 35
Stefan Ferstl Avatar answered Sep 27 '22 11:09

Stefan Ferstl