Backend Development 6 min read

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.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Boost Spring Startup Speed with spring-context-indexer: A Complete Guide

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>&lt;dependency&gt;
  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
  &lt;artifactId&gt;spring-context-indexer&lt;/artifactId&gt;
  &lt;version&gt;5.3.23&lt;/version&gt;
  &lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
</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.

Javastartup optimizationSpringdependency injectionspring-context-indexer
Spring Full-Stack Practical Cases
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.