diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshEndpointAutoConfiguration.java b/spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshEndpointAutoConfiguration.java index 243795bf..09c1cb3b 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshEndpointAutoConfiguration.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshEndpointAutoConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.cloud.autoconfigure; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint; @@ -52,11 +53,10 @@ import org.springframework.integration.monitor.IntegrationMBeanExporter; public class RefreshEndpointAutoConfiguration { @Bean - @ConditionalOnBean(RefreshScope.class) @ConditionalOnMissingBean @ConditionalOnEnabledHealthIndicator("refresh") - RefreshScopeHealthIndicator refreshScopeHealthIndicator(RefreshScope scope, - ConfigurationPropertiesRebinder rebinder) { + RefreshScopeHealthIndicator refreshScopeHealthIndicator(ObjectProvider scope, + ConfigurationPropertiesRebinder rebinder) { return new RefreshScopeHealthIndicator(scope, rebinder); } diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/health/RefreshScopeHealthIndicator.java b/spring-cloud-context/src/main/java/org/springframework/cloud/health/RefreshScopeHealthIndicator.java index b8df5e1b..a8de8b4f 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/health/RefreshScopeHealthIndicator.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/health/RefreshScopeHealthIndicator.java @@ -19,6 +19,7 @@ package org.springframework.cloud.health; import java.util.HashMap; import java.util.Map; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.actuate.health.AbstractHealthIndicator; import org.springframework.boot.actuate.health.Health.Builder; import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder; @@ -33,18 +34,18 @@ import org.springframework.cloud.context.scope.refresh.RefreshScope; */ public class RefreshScopeHealthIndicator extends AbstractHealthIndicator { - private RefreshScope scope; + private ObjectProvider scope; private ConfigurationPropertiesRebinder rebinder; - public RefreshScopeHealthIndicator(RefreshScope scope, - ConfigurationPropertiesRebinder rebinder) { + public RefreshScopeHealthIndicator(ObjectProvider scope, + ConfigurationPropertiesRebinder rebinder) { this.scope = scope; this.rebinder = rebinder; } @Override protected void doHealthCheck(Builder builder) throws Exception { - Map errors = new HashMap<>(this.scope.getErrors()); + Map errors = new HashMap<>(this.scope.getIfAvailable().getErrors()); errors.putAll(this.rebinder.getErrors()); if (errors.isEmpty()) { builder.up(); diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/health/RefreshScopeHealthIndicatorTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/health/RefreshScopeHealthIndicatorTests.java index 3749ef95..4dbb5908 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/health/RefreshScopeHealthIndicatorTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/health/RefreshScopeHealthIndicatorTests.java @@ -20,12 +20,14 @@ import java.util.Collections; import org.junit.Before; import org.junit.Test; -import org.mockito.Mockito; +import org.mockito.BDDMockito; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.actuate.health.Status; import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder; import org.springframework.cloud.context.scope.refresh.RefreshScope; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** @@ -33,18 +35,20 @@ import static org.mockito.Mockito.when; */ public class RefreshScopeHealthIndicatorTests { - private ConfigurationPropertiesRebinder rebinder = Mockito - .mock(ConfigurationPropertiesRebinder.class); - private RefreshScope scope = Mockito.mock(RefreshScope.class); + private ObjectProvider scopeProvider = mock(ObjectProvider.class); + private ConfigurationPropertiesRebinder rebinder = + mock(ConfigurationPropertiesRebinder.class); + private RefreshScope scope = mock(RefreshScope.class); private RefreshScopeHealthIndicator indicator = new RefreshScopeHealthIndicator( - this.scope, this.rebinder); + this.scopeProvider, this.rebinder); @Before public void init() { + BDDMockito.willReturn(scope).given(scopeProvider).getIfAvailable(); when(this.rebinder.getErrors()) - .thenReturn(Collections. emptyMap()); + .thenReturn(Collections.emptyMap()); when(this.scope.getErrors()) - .thenReturn(Collections. emptyMap()); + .thenReturn(Collections.emptyMap()); } @Test @@ -55,23 +59,23 @@ public class RefreshScopeHealthIndicatorTests { @Test public void binderError() { when(this.rebinder.getErrors()).thenReturn(Collections - . singletonMap("foo", new RuntimeException("FOO"))); + .singletonMap("foo", new RuntimeException("FOO"))); assertEquals(Status.DOWN, this.indicator.health().getStatus()); } @Test public void scopeError() { when(this.scope.getErrors()).thenReturn(Collections - . singletonMap("foo", new RuntimeException("FOO"))); + .singletonMap("foo", new RuntimeException("FOO"))); assertEquals(Status.DOWN, this.indicator.health().getStatus()); } @Test public void bothError() { when(this.rebinder.getErrors()).thenReturn(Collections - . singletonMap("foo", new RuntimeException("FOO"))); + .singletonMap("foo", new RuntimeException("FOO"))); when(this.scope.getErrors()).thenReturn(Collections - . singletonMap("bar", new RuntimeException("BAR"))); + .singletonMap("bar", new RuntimeException("BAR"))); assertEquals(Status.DOWN, this.indicator.health().getStatus()); }