BAEL-4871 Spring Conditional Annotations

This commit is contained in:
majewsk6 2021-05-16 20:16:33 +02:00
parent 9d5b3fcedb
commit 4d08dcace0
8 changed files with 16 additions and 17 deletions

View File

@ -1,4 +1,4 @@
package java.com.baeldung.annotations.conditional; package com.baeldung.annotations.conditional;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;

View File

@ -1,4 +1,4 @@
package java.com.baeldung.annotations.conditional; package com.baeldung.annotations.conditional;
import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.ConditionContext;

View File

@ -1,4 +1,4 @@
package java.com.baeldung.annotations.conditional; package com.baeldung.annotations.conditional;
import org.apache.commons.lang3.SystemUtils; import org.apache.commons.lang3.SystemUtils;
@ -10,6 +10,6 @@ public class IsWindowsCondition implements Condition {
@Override @Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return SystemUtils.IS_OS_WINDOWS;; return SystemUtils.IS_OS_WINDOWS;
} }
} }

View File

@ -1,6 +1,6 @@
package java.com.baeldung.annotations.conditional; package com.baeldung.annotations.conditional;
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava; import org.springframework.boot.system.JavaVersion;
import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.core.type.AnnotatedTypeMetadata;
@ -9,6 +9,6 @@ public class Java8Condition implements Condition {
@Override @Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return ConditionalOnJava.JavaVersion.getJavaVersion().equals(ConditionalOnJava.JavaVersion.EIGHT); return JavaVersion.getJavaVersion().equals(JavaVersion.EIGHT);
} }
} }

View File

@ -1,4 +1,4 @@
package java.com.baeldung.annotations.conditional; package com.baeldung.annotations.conditional;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;

View File

@ -1,4 +1,4 @@
package java.com.baeldung.annotations.conditional; package com.baeldung.annotations.conditional;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;

View File

@ -1,6 +1,6 @@
package java.com.baeldung.annotations.conditional; package com.baeldung.annotations.conditional;
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava; import org.springframework.boot.system.JavaVersion;
import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.core.type.AnnotatedTypeMetadata;
@ -9,6 +9,6 @@ public class Java9Condition implements Condition {
@Override @Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return ConditionalOnJava.JavaVersion.getJavaVersion().equals(ConditionalOnJava.JavaVersion.NINE); return JavaVersion.getJavaVersion().equals(JavaVersion.NINE);
} }
} }

View File

@ -1,20 +1,19 @@
package java.com.baeldung.annotations.conditional; package com.baeldung.annotations.conditional;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava; import org.springframework.boot.autoconfigure.condition.ConditionalOnJava;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.system.JavaVersion;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
@Conditional(IsDevEnvCondition.class) @Conditional({IsDevEnvCondition.class, IsWindowsCondition.class, Java8Condition.class})
@ConditionalOnProperty( @ConditionalOnProperty(
value = "logging.enabled", value = "logging.enabled",
havingValue = "true", havingValue = "true",
matchIfMissing = true) matchIfMissing = true)
@ConditionalOnExpression("${logging.enabled:true} and ${logging.level:DEBUG}") @ConditionalOnExpression("${logging.enabled:true} and ${logging.level:DEBUG}")
@ConditionalOnJava(ConditionalOnJava.JavaVersion.EIGHT) @ConditionalOnJava(JavaVersion.EIGHT)
@Conditional(IsWindowsCondition.class)
@Conditional(Java8Condition.class)
public class LoggingService { public class LoggingService {
} }