diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/condition/OnEnabledEndpointCondition.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/condition/OnEnabledEndpointCondition.java index f58be42cfe..6345802ff6 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/condition/OnEnabledEndpointCondition.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/condition/OnEnabledEndpointCondition.java @@ -16,6 +16,8 @@ package org.springframework.boot.actuate.autoconfigure.endpoint.condition; +import java.util.Optional; + import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.EndpointExtension; import org.springframework.boot.autoconfigure.condition.ConditionMessage; @@ -25,10 +27,12 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.AnnotationAttributes; +import org.springframework.core.env.Environment; import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.core.type.MethodMetadata; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.ConcurrentReferenceHashMap; /** * A condition that checks if an endpoint is enabled. @@ -42,22 +46,23 @@ class OnEnabledEndpointCondition extends SpringBootCondition { private static final String ENABLED_BY_DEFAULT_KEY = "management.endpoints.enabled-by-default"; + private static final ConcurrentReferenceHashMap> enabledByDefaultCache = new ConcurrentReferenceHashMap<>(); + @Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { + Environment environment = context.getEnvironment(); AnnotationAttributes attributes = getEndpointAttributes(context, metadata); String id = attributes.getString("id"); String key = "management.endpoint." + id + ".enabled"; - Boolean userDefinedEnabled = context.getEnvironment().getProperty(key, - Boolean.class); + Boolean userDefinedEnabled = environment.getProperty(key, Boolean.class); if (userDefinedEnabled != null) { return new ConditionOutcome(userDefinedEnabled, ConditionMessage.forCondition(ConditionalOnEnabledEndpoint.class) .because("found property " + key + " with value " + userDefinedEnabled)); } - Boolean userDefinedDefault = context.getEnvironment() - .getProperty(ENABLED_BY_DEFAULT_KEY, Boolean.class); + Boolean userDefinedDefault = isEnabledByDefault(environment); if (userDefinedDefault != null) { return new ConditionOutcome(userDefinedDefault, ConditionMessage.forCondition(ConditionalOnEnabledEndpoint.class) @@ -71,6 +76,16 @@ class OnEnabledEndpointCondition extends SpringBootCondition { "no property " + key + " found so using endpoint default")); } + private Boolean isEnabledByDefault(Environment environment) { + Optional enabledByDefault = enabledByDefaultCache.get(environment); + if (enabledByDefault == null) { + enabledByDefault = Optional.ofNullable( + environment.getProperty(ENABLED_BY_DEFAULT_KEY, Boolean.class)); + enabledByDefaultCache.put(environment, enabledByDefault); + } + return enabledByDefault.orElse(null); + } + private AnnotationAttributes getEndpointAttributes(ConditionContext context, AnnotatedTypeMetadata metadata) { Assert.state(