Sub topic 9
Typed settings for your auto-configuration
This chapter assumes you know application.properties, @Configuration and @Bean classes, and the GreetingAutoConfiguration shape from earlier chapters. Here you will let users configure your auto-configuration through a typed class instead of scattered @Value strings.
Why a properties class
@Value("${greeting.message}") works, but each key is a loose string. Nothing groups the keys, and the IDE cannot suggest them. A @ConfigurationProperties class gives you one object per prefix, with typed fields, defaults and validation. Spring Boot binds values from the Environment onto it, so properties files, environment variables and command-line arguments all work.
The properties class
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;
}
}The field initializer "Hello" is the default. If no greeting.message exists, the binder leaves the field alone and the default stays. The binder uses the setter, so a JavaBean-style class needs a getter and a setter for each property.
Registering it from the auto-configuration
An auto-configuration is not component-scanned, so annotating the properties class with @Component is the wrong approach. Register it with @EnableConfigurationProperties:
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());
}
}@EnableConfigurationProperties registers GreetingProperties as a bean and binds it. The @Bean method receives it as a parameter, like any other dependency.
Setting the values
A user can now write this in application.properties:
greeting.message=WelcomeRelaxed binding also accepts the environment variable GREETING_MESSAGE=Welcome. For a multi-word field such as maxLength, the kebab-case key greeting.max-length is the recommended form in properties files.
Why `enabled` is not a field
The greeting.enabled switch is read by @ConditionalOnProperty, not by GreetingProperties. Conditions are evaluated while bean definitions are being registered, before any bean such as GreetingProperties exists. A condition can therefore only look at the Environment. Use a properties field for values consumed by beans, and use @ConditionalOnProperty for switches that decide whether beans exist at all.
Adding metadata for the IDE
Add the configuration processor so the build generates META-INF/spring-configuration-metadata.json. IDEs use it for completion and for documentation of each key.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>In Gradle, declare it as annotationProcessor "org.springframework.boot:spring-boot-configuration-processor".
Common errors
- Value stays at the default. The prefix is misspelled, or the setter is missing. The binder ignores unknown keys silently by default.
- No bean of type
GreetingProperties. The class is not registered. Add@EnableConfigurationPropertiesto the auto-configuration. - No completion in the IDE. The configuration processor is missing, or the project has not been rebuilt.
Exercise
A user sets greeting.message=Hi but GreetingService still says "Hello". GreetingProperties has a getter but no setter. What is the likely cause, and what is the fix?
Show answer
With the JavaBean style of binding, the binder sets each value through a setter. Without setMessage, the value is never applied and the default "Hello" remains. Adding the setter fixes it.