Skip to main content
PreparationZone Logo
PrepZone

@Conditional and Custom @Configuration

Write your own auto-configuration with conditional annotations and @ConfigurationProperties.

Pass checkpoint below

Read these first

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.
  • @ConfigurationProperties combined 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 · Intermediate

Enable BookStore's recommendation client only when bookstore.recommendations.enabled=true. Keeps optional integrations off in tests and small environments.

Custom auto-configuration module

L3 · Advanced

Package @AutoConfiguration with conditions and META-INF/spring/...AutoConfiguration.imports to ship reusable config like official starters.

Configuration metadata

L4 · Expert

Add spring-boot-configuration-processor for IDE hints on @ConfigurationProperties. Document every property in additional-spring-configuration-metadata.json for ops teams.

Classpath scanFind @AutoConfiguration classes
@Conditional checksOnClass, OnBean, OnProperty
Register beansOnly if conditions pass
Boot checks classpath and existing beans before creating defaults.

Building a custom auto-configuration

Suppose BookStore integrates with an external recommendation engine:

Java
@ConfigurationProperties(prefix = "bookstore.recommendations")
public record RecommendationProperties(
    boolean enabled,
    String apiUrl,
    Duration timeout
) {}
Java
// 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:

Java
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 via application.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.