Why this matters
- Custom auto-configuration lets teams share BookStore client libraries across microservices with zero manual bean registration.
- Conditions prevent beans from loading in the wrong environment — a Redis cache bean should not start when Redis is absent.
@ConfigurationPropertiescombined with conditions creates type-safe, environment-aware configuration.
@Configuration classes
L1 · Fresher@Configuration classes define @Bean methods for third-party clients or beans you cannot annotate with @Component.
@ConditionalOnProperty
L2 · IntermediateEnable BookStore's recommendation client only when bookstore.recommendations.enabled=true. Keeps optional integrations off in tests and small environments.
Custom auto-configuration module
L3 · AdvancedPackage @AutoConfiguration with conditions and META-INF/spring/...AutoConfiguration.imports to ship reusable config like official starters.
Configuration metadata
L4 · ExpertAdd spring-boot-configuration-processor for IDE hints on @ConfigurationProperties. Document every property in additional-spring-configuration-metadata.json for ops teams.
Building a custom auto-configuration
Suppose BookStore integrates with an external recommendation engine:
@ConfigurationProperties(prefix = "bookstore.recommendations")
public record RecommendationProperties(
boolean enabled,
String apiUrl,
Duration timeout
) {}
// learn:fragment
@AutoConfiguration
@ConditionalOnClass(RecommendationClient.class)
@ConditionalOnProperty(
prefix = "bookstore.recommendations",
name = "enabled",
havingValue = "true"
)
@EnableConfigurationProperties(RecommendationProperties.class)
public class RecommendationAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public RecommendationClient recommendationClient(RecommendationProperties props) {
return RecommendationClient.builder()
.baseUrl(props.apiUrl())
.timeout(props.timeout())
.build();
}
}
Register in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports:
com.example.bookstore.recommendations.RecommendationAutoConfiguration
Consumers add the library JAR and set properties — the client bean appears automatically.
Condition types for custom config
@ConditionalOnClass— Feature available only when a library is present.@ConditionalOnMissingBean— User can override with their own@Bean.@ConditionalOnProperty— Toggle features viaapplication.yml.@ConditionalOnProfile— Environment-specific beans (staging,prod).@ConditionalOnBean— Activate only when another bean exists.
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 6 sections
- Custom condition class
- @ConfigurationProperties validation
- Multiple property sources
- Ordering custom auto-config
- Testing conditional beans
- Feature flags in BookStore
Test yourself
Answer these before moving on — recall is what makes it stick.