Boost Spring Startup Speed with spring-context-indexer: A Complete Guide
Learn how to dramatically reduce startup time in large Spring applications by using spring-context-indexer to generate component indexes at compile time, configure Maven or Gradle dependencies, create manual or IDE‑generated index files, support custom annotations, and disable indexing when needed.
Environment: Spring 5.3.23
1. Introduction
In large Spring projects, registering hundreds of beans via class‑path scanning can slow down application startup. The spring‑context‑indexer creates an index of components at compile time, allowing the container to load them quickly and significantly improve startup speed.
2. Configuration
Adding the Maven dependency
<code><dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
<version>5.3.23</version>
<optional>true</optional>
</dependency>
</code>If you use Gradle:
<code># Gradle 4.5 and below
dependencies {
compileOnly "org.springframework:spring-context-indexer:5.3.23"
}
# Gradle 4.6 and above
dependencies {
annotationProcessor "org.springframework:spring-context-indexer:5.3.23"
}
</code>Preparing bean classes
<code>@Component
public class Person { }
@Component
public class Student { }
@Component
public class User { }
</code>Testing that the classes are discovered
<code>try (AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.pack.context_indexed")) {
for (String name : context.getBeanDefinitionNames()) {
System.out.println(name);
}
}
</code>Console output shows the framework’s internal processors followed by person , student , and user , confirming that all beans are scanned.
Manual index file creation
Create META-INF/spring.components with entries in the form full.package.Class=full.annotation.Name :
<code>com.pack.context_indexed.Person=org.springframework.stereotype.Component
</code>After adding this file, only person is loaded because the index lists only that bean.
Custom annotation support
<code>// Custom annotation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component
public @interface PackComponent { }
// Apply to a bean
@PackComponent
public class User { }
</code>Add the custom mapping to the index file:
<code>com.pack.context_indexed.Person=org.springframework.stereotype.Component
com.pack.context_indexed.User=com.pack.context_indexed.PackComponent
</code>Running the test now prints person and user .
Automatic index generation (Eclipse example)
Configure the spring‑context‑indexer as an annotation processor in Eclipse. After building, the spring.components file is generated under target/classes/META-INF with entries for all @Component classes:
<code>com.pack.context_indexed.Person=org.springframework.stereotype.Component
com.pack.context_indexed.Student=org.springframework.stereotype.Component
com.pack.context_indexed.User=org.springframework.stereotype.Component
</code>Disabling the index
Set the JVM option -Dspring.index.ignore=true to turn off indexing.
In summary, spring‑context‑indexer reduces startup time in large Spring applications by generating a compile‑time component index, which the container can read quickly, leading to faster development cycles, lower resource consumption, and fewer runtime errors.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.