Sub topic 10
Writing your own auto-configuration
This chapter assumes you know @Configuration and @Bean, can run a basic Spring Boot application, and have used application.properties. It also assumes you've seen how @ConditionalOnClass, @ConditionalOnMissingBean and @ConditionalOnProperty behave. Here you combine them into a small library module that other applications can use.
The goal
You will build a greeting module. An application that adds it as a dependency gets a GreetingService bean without writing any configuration. The application can still:
- change the message with a property,
- turn the feature off with a property,
- replace the bean with its own.
The module needs four pieces: the service, a properties class, the auto-configuration class, and an imports file that registers it.
Step 1: The service
The service is a plain class. It does not depend on Spring.
package com.example.greeting;
public class GreetingService {
private final String message;
public GreetingService(String message) {
this.message = message;
}
public String greet(String name) {
return message + ", " + name + "!";
}
}Step 2: The properties class
@ConfigurationProperties binds greeting.message from the environment. If the property is absent, the field default Hello is used.
package com.example.greeting;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "greeting")
public class GreetingProperties {
private String message = "Hello";
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}Step 3: The auto-configuration class
package com.example.greeting;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@AutoConfiguration
@ConditionalOnClass(GreetingService.class)
@ConditionalOnProperty(prefix = "greeting", name = "enabled", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(GreetingProperties.class)
public class GreetingAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public GreetingService greetingService(GreetingProperties properties) {
return new GreetingService(properties.getMessage());
}
}Each annotation does one job:
| Annotation | Effect |
|---|---|
| `@AutoConfiguration` | Marks the class as an auto-configuration. It also lets you set `before` and `after`. |
| `@ConditionalOnClass` | Skips the whole class unless `GreetingService` is on the classpath. |
| `@ConditionalOnProperty` | Skips the class when `greeting.enabled=false`. `matchIfMissing = true` makes the feature on by default. |
| `@EnableConfigurationProperties` | Registers `GreetingProperties` as a bean so binding happens. |
| `@ConditionalOnMissingBean` | Skips the bean method if the application already defines a `GreetingService`. |
Step 4: Register the class
Writing the class is not enough. Spring Boot only considers classes listed in a file inside your module. Create src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports with one fully qualified class name per line:
com.example.greeting.GreetingAutoConfigurationDepending on other auto-configurations
Put @ConditionalOnBean only on auto-configuration classes, because auto-configurations are processed after the application's own beans are registered. If your class needs a bean that another auto-configuration creates, order it explicitly with after:
package com.example.audit;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
@AutoConfiguration(after = DataSourceAutoConfiguration.class)
@ConditionalOnBean(DataSource.class)
public class AuditAutoConfiguration {
@Bean
public AuditLogger auditLogger(DataSource dataSource) {
return new AuditLogger(dataSource);
}
}Without after, the DataSource definition may not exist yet when the condition runs. The condition then fails and the bean is silently skipped.
Common mistakes
- Forgetting the imports file. The class compiles and never runs. Check the file name and path character by character.
- Using
@ConditionalOnMissingBeanon a user's own@Configuration. It is only reliable in auto-configurations, which are processed last. - Putting
@ConditionalOnClasson a method's return type only. Class loading can fail before the condition is read. Put the condition on the class, as above.
Exercise
A teammate adds your greeting module as a dependency and defines their own GreetingService bean. Which annotation makes your auto-configuration use theirs, and what must be true about when your class is processed?
Show answer
@ConditionalOnMissingBean on the greetingService method makes it back off. It works because auto-configurations are processed after the application's own bean definitions, so the user's bean is already registered when the condition is evaluated.