Sub topic 6
Tuning defaults with properties and @ConfigurationProperties
Auto-configuration picks sensible defaults, and properties let you change them without writing code. This chapter shows how Boot reads properties, how @ConfigurationProperties binds them to a class, and how that fits into auto-configuration.
This chapter assumes you know beans, the application context, and @Configuration and @Bean. It also assumes you can run a simple Spring Boot application.
Why properties matter to auto-configuration
An auto-configuration class creates beans from the classpath. Many of those beans need values, such as a port, a pool size, or a message. Hard-coding these values would force users to replace the whole bean. Boot instead reads values from the Environment and lets users override them in application.properties, application.yml, environment variables, or command-line arguments.
A well-written auto-configuration therefore does two things:
- It supplies a default for every setting.
- It exposes each setting under a documented property prefix.
You have already used this. server.port=9090 changes the web server port without touching any bean definition.
Binding properties to a class
You could read each value with @Value("${greeting.message}"). That works for one or two values, but it scatters property names across your code. @ConfigurationProperties groups related settings into one typed class.
@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 the user sets greeting.message=Welcome, Boot calls setMessage("Welcome"). If the user sets nothing, the default stays.
Boot does not register this class as a bean on its own. You must enable it, either with @EnableConfigurationProperties(GreetingProperties.class) on a configuration class or with @ConfigurationPropertiesScan on the application.
@AutoConfiguration
@ConditionalOnClass(GreetingService.class)
@EnableConfigurationProperties(GreetingProperties.class)
public class GreetingAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public GreetingService greetingService(GreetingProperties properties) {
return new GreetingService(properties.getMessage());
}
}The properties bean is injected into the factory method, so the service is built from whatever the user configured.
Now the user changes behavior in one line:
# application.properties
greeting.message=WelcomeWith that line, greetingService.greet("Ann") returns Welcome, Ann!.
Relaxed binding
Boot matches property names loosely. For a field named maxRetries under the prefix greeting, all of these bind to it:
| Source | Example |
|---|---|
| Properties file (recommended) | `greeting.max-retries=3` |
| Camel case | `greeting.maxRetries=3` |
| Underscore | `greeting.max_retries=3` |
| Environment variable | `GREETING_MAXRETRIES=3` |
Use kebab case (max-retries) in files. It is the form Boot's own documentation uses. Use upper-case with underscores for environment variables.
The prefix itself must be in kebab case, lower case, such as greeting or my-app. A prefix written in camel case will not bind.
Type conversion and validation
Boot converts strings to the field's type. It handles numbers, booleans, enums, Duration (30s, 5m), DataSize (10MB), lists, and nested objects.
private Duration timeout = Duration.ofSeconds(5);
private List<String> names = new ArrayList<>();For validation, add @Validated to the properties class and use Bean Validation annotations such as @NotBlank or @Min. This needs a validation implementation on the classpath, for example spring-boot-starter-validation. An invalid value then stops startup with a clear message instead of failing later at runtime.
Common errors
- The property is ignored. The class is not registered. Add
@EnableConfigurationPropertiesor@ConfigurationPropertiesScan. - The value stays at its default. The class has no setter (or constructor binding) for the field, or the prefix is spelled differently than the property key.
- Startup fails with a binding error. The value cannot convert to the field type, such as
abcfor anint. - Typos pass silently.
greeting.mesage=Hiis just an unknown property. Boot does not warn unless you add thespring-boot-configuration-processordependency, which generates metadata so IDEs can flag unknown keys and offer completion.
Exercise
A user sets greeting.message=Welcome in application.properties, but GreetingService still greets with "Hello". The GreetingProperties class has the correct prefix, a getter, and a setter. Name two things to check.
Show answer
First, check that the class is registered, with @EnableConfigurationProperties(GreetingProperties.class) or @ConfigurationPropertiesScan. Second, check that the service is built from the properties bean, and that a user-defined GreetingService bean is not causing the auto-configured one to back off (@ConditionalOnMissingBean) with a hard-coded message.