BAEL-4871 Spring Conditional Annotations

This commit is contained in:
majewsk6 2021-05-16 20:02:27 +02:00
parent 67445ed3c1
commit 6f757f33b3
8 changed files with 119 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -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");;
}
}

View File

@ -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;;
}
}

View File

@ -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);
}
}

View File

@ -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 {
}

View File

@ -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 {
}
}

View File

@ -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);
}
}

View File

@ -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 {
}