Using @Conditional with @Configuration in Spring allows us to group @Bean objects loaded on a specific condition without resorting to using @Profile and too many @Bean/@Conditional.
Requirements
We used the following items for this post.
- Spring Boot 2.0.4.RELEASE
- JDK 8
- IntelliJ IDEA
Grouping Beans into Two in Spring
1. All-English Configuration
The @Configuration class contains all beans that give out String values in English. For example, consider the following codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.turreta.conditionalconfiguration.conditionalconfiguration; import com.turreta.conditionalconfiguration.conditionalconfiguration.condition.EnglishLanguageCondition; import com.turreta.conditionalconfiguration.conditionalconfiguration.credits.CreditsEnglish; import com.turreta.conditionalconfiguration.conditionalconfiguration.helloworld.HelloWorldEnglish; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; @Configuration @Conditional(EnglishLanguageCondition.class) public class AllEnglishConfiguration { @Bean public CreditsEnglish creditsEnglish() { return new CreditsEnglish(); } @Bean public HelloWorldEnglish helloWorldEnglish() { return new HelloWorldEnglish(); } } |
The Condition implementation for this is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.turreta.conditionalconfiguration.conditionalconfiguration.condition; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; public class EnglishLanguageCondition implements Condition { @Override public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { // Don't check for actual value String lang = conditionContext.getEnvironment().getProperty("english"); return lang != null; } } |
2. All-German Configuration
This @Configuration class contains all beans that give out String values in the German language. Consider the following codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.turreta.conditionalconfiguration.conditionalconfiguration; import com.turreta.conditionalconfiguration.conditionalconfiguration.condition.GermanLanguageCondition; import com.turreta.conditionalconfiguration.conditionalconfiguration.credits.CreditsGerman; import com.turreta.conditionalconfiguration.conditionalconfiguration.helloworld.HelloWorldGerman; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; @Configuration @Conditional(GermanLanguageCondition.class) public class AllGermanConfiguration { @Bean public CreditsGerman creditsGerman() { return new CreditsGerman(); } @Bean public HelloWorldGerman helloWorldGerman() { return new HelloWorldGerman(); } } |
The Condition implementation for this is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.turreta.conditionalconfiguration.conditionalconfiguration.condition; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; public class GermanLanguageCondition implements Condition { @Override public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { // Don't check for actual value String lang = conditionContext.getEnvironment().getProperty("german"); return lang != null; } } |
Test @Conditional and @Configuration in Spring
To toggle between languages, we explicitly set either one of the system properties – “English” or “German” – and run within the IDE.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.turreta.conditionalconfiguration.conditionalconfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class ConditionalconfigurationApplication { public static void main(String[] args) { System.setProperty("german", "any value"); //System.setProperty("english", "any value"); final ConfigurableApplicationContext context = SpringApplication .run(ConditionalconfigurationApplication.class, args); System.out.println(context.getBean(HelloWorld.class).getGreeting()); System.out.println(context.getBean(Credits.class).getCredits()); } } |
Then, we can test our codes by switching between languages using a property. For example, when we want to display a message in German, we could use the “German” system property, which is equivalent to -Dgerman="any value".
1 2 3 | ... System.setProperty("german", "any value"); ... |
Meanwhile, when we want to display a message in English, we use the “English” system property. Also, this is equivalent to -Denglish="any value".
1 | System.setProperty("english", "any value"); |
Download Spring @Conditional and @Configuration Sample Codes
For more information, please download the codes from this GitHub repository.