diff --git a/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/DevEnvLoggingConfiguration.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/DevEnvLoggingConfiguration.java new file mode 100644 index 0000000000..c2134ab053 --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/DevEnvLoggingConfiguration.java @@ -0,0 +1,16 @@ +package java.com.baeldung.annotations.conditional; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Conditional; +import org.springframework.context.annotation.Configuration; + +@Configuration +@Conditional(IsDevEnvCondition.class) +public class DevEnvLoggingConfiguration { + + @Bean + @Conditional(IsDevEnvCondition.class) + LoggingService loggingService() { + return new LoggingService(); + } +} diff --git a/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/IsDevEnvCondition.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/IsDevEnvCondition.java new file mode 100644 index 0000000000..e0da72d731 --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/IsDevEnvCondition.java @@ -0,0 +1,13 @@ +package java.com.baeldung.annotations.conditional; + +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; + +public class IsDevEnvCondition implements Condition { + + @Override + public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + return System.getProperty("env").equals("dev");; + } +} diff --git a/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/IsWindowsCondition.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/IsWindowsCondition.java new file mode 100644 index 0000000000..d7e492b5af --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/IsWindowsCondition.java @@ -0,0 +1,15 @@ +package java.com.baeldung.annotations.conditional; + +import org.apache.commons.lang3.SystemUtils; + +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; + +public class IsWindowsCondition implements Condition { + + @Override + public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + return SystemUtils.IS_OS_WINDOWS;; + } +} diff --git a/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/Java8Condition.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/Java8Condition.java new file mode 100644 index 0000000000..a18093ce18 --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/Java8Condition.java @@ -0,0 +1,14 @@ +package java.com.baeldung.annotations.conditional; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnJava; +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; + +public class Java8Condition implements Condition { + + @Override + public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + return ConditionalOnJava.JavaVersion.getJavaVersion().equals(ConditionalOnJava.JavaVersion.EIGHT); + } +} diff --git a/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/Java8DependedService.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/Java8DependedService.java new file mode 100644 index 0000000000..bd2bdff99a --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/Java8DependedService.java @@ -0,0 +1,9 @@ +package java.com.baeldung.annotations.conditional; + +import org.springframework.context.annotation.Conditional; +import org.springframework.stereotype.Service; + +@Service +@Conditional(Java8Condition.class) +public class Java8DependedService { +} diff --git a/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/Java8OrJava9.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/Java8OrJava9.java new file mode 100644 index 0000000000..5a19d7ef77 --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/Java8OrJava9.java @@ -0,0 +1,18 @@ +package java.com.baeldung.annotations.conditional; + +import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; +import org.springframework.context.annotation.Conditional; + +public class Java8OrJava9 extends AnyNestedCondition { + Java8OrJava9() { + super(ConfigurationPhase.REGISTER_BEAN); + } + + @Conditional(Java8Condition.class) + static class Java8 { + } + + @Conditional(Java9Condition.class) + static class Java9 { + } +} diff --git a/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/Java9Condition.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/Java9Condition.java new file mode 100644 index 0000000000..2f9c29d9ec --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/Java9Condition.java @@ -0,0 +1,14 @@ +package java.com.baeldung.annotations.conditional; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnJava; +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; + +public class Java9Condition implements Condition { + + @Override + public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + return ConditionalOnJava.JavaVersion.getJavaVersion().equals(ConditionalOnJava.JavaVersion.NINE); + } +} diff --git a/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/LoggingService.java b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/LoggingService.java new file mode 100644 index 0000000000..e9aaaf3f74 --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations/src/main/java/com/baeldung/annotations/conditional/LoggingService.java @@ -0,0 +1,20 @@ +package java.com.baeldung.annotations.conditional; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.boot.autoconfigure.condition.ConditionalOnJava; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Conditional; +import org.springframework.stereotype.Service; + +@Service +@Conditional(IsDevEnvCondition.class) +@ConditionalOnProperty( + value = "logging.enabled", + havingValue = "true", + matchIfMissing = true) +@ConditionalOnExpression("${logging.enabled:true} and ${logging.level:DEBUG}") +@ConditionalOnJava(ConditionalOnJava.JavaVersion.EIGHT) +@Conditional(IsWindowsCondition.class) +@Conditional(Java8Condition.class) +public class LoggingService { +}