BAEL-4871 Spring Conditional Annotations
This commit is contained in:
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.apache.commons.lang3.SystemUtils;
|
||||
import org.springframework.boot.system.JavaVersion;
|
||||
|
||||
public class ConditionalUtils {
|
||||
|
||||
public static boolean isWindows() {
|
||||
return SystemUtils.IS_OS_WINDOWS;
|
||||
}
|
||||
|
||||
public static boolean isJava8() {
|
||||
return JavaVersion.getJavaVersion().equals(JavaVersion.EIGHT);
|
||||
}
|
||||
|
||||
public static boolean isJava9() {
|
||||
return JavaVersion.getJavaVersion().equals(JavaVersion.NINE);
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -8,6 +8,6 @@ public class IsDevEnvCondition implements Condition {
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
return System.getProperty("env").equals("dev");
|
||||
return "dev".equals(System.getProperty("env"));
|
||||
}
|
||||
}
|
||||
|
||||
+1
-3
@@ -1,7 +1,5 @@
|
||||
package 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;
|
||||
@@ -10,6 +8,6 @@ public class IsWindowsCondition implements Condition {
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
return SystemUtils.IS_OS_WINDOWS;
|
||||
return ConditionalUtils.isWindows();
|
||||
}
|
||||
}
|
||||
|
||||
+1
-2
@@ -1,6 +1,5 @@
|
||||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.springframework.boot.system.JavaVersion;
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
@@ -9,6 +8,6 @@ public class Java8Condition implements Condition {
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
return JavaVersion.getJavaVersion().equals(JavaVersion.EIGHT);
|
||||
return ConditionalUtils.isJava8();
|
||||
}
|
||||
}
|
||||
|
||||
+1
-2
@@ -1,6 +1,5 @@
|
||||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.springframework.boot.system.JavaVersion;
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
@@ -9,6 +8,6 @@ public class Java9Condition implements Condition {
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
return JavaVersion.getJavaVersion().equals(JavaVersion.NINE);
|
||||
return ConditionalUtils.isJava9();
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ import org.springframework.stereotype.Service;
|
||||
value = "logging.enabled",
|
||||
havingValue = "true",
|
||||
matchIfMissing = true)
|
||||
@ConditionalOnExpression("${logging.enabled:true} and ${logging.level:DEBUG}")
|
||||
@ConditionalOnExpression("${logging.enabled:true} and '${logging.level}'.equals('DEBUG')")
|
||||
@ConditionalOnJava(JavaVersion.EIGHT)
|
||||
public class LoggingService {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user