BAEL-4871 Spring Conditional Annotations
This commit is contained in:
parent
67445ed3c1
commit
6f757f33b3
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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");;
|
||||
}
|
||||
}
|
|
@ -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;;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
}
|
|
@ -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 {
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
}
|
Loading…
Reference in New Issue