Sub topic 3
Typed settings and testing custom auto-configuration
Assume you know Java, dependency injection, @Bean, Spring Boot configuration, and how to run a Boot application.
Bind settings with `@ConfigurationProperties`
@ConfigurationProperties maps environment values to a typed Java object. This keeps property names and defaults in one place.
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;
}
}Spring binds greeting.message from application.properties. Your auto-configuration can then inject GreetingProperties instead of reading strings directly from the environment.
Write custom auto-configuration
Use conditions to provide a default while allowing application code to replace it.
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
GreetingService greetingService(GreetingProperties properties) {
return new GreetingService(properties.getMessage());
}
}List the class in src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports:
com.example.greeting.GreetingAutoConfiguration@ConditionalOnMissingBean makes the configuration back off when the application supplies its own GreetingService. A @ConditionalOnBean check depends on processing order, so use it mainly in auto-configuration classes. If another auto-configuration creates the required bean first, use @AutoConfiguration(after = OtherAutoConfiguration.class).
Diagnose and exclude configuration
Run the application with --debug:
java -jar app.jar --debugThe conditions evaluation report shows positive matches, negative matches, and the reason each condition failed. Check the report before changing conditions.
Exclude an unwanted configuration explicitly:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class Application {
}You can also exclude configurations through the spring.autoconfigure.exclude property.
Test in isolation
ApplicationContextRunner creates a small context containing only the auto-configuration under test. Test the default, disabled, and user-defined cases.
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(GreetingAutoConfiguration.class))
.withPropertyValues("greeting.enabled=true")
.run(context -> assertThat(context).hasSingleBean(GreetingService.class));Exercise
Which annotation lets a custom auto-configuration provide a default bean without replacing the application's bean?
Show answer
@ConditionalOnMissingBean.