From b40ceef356aa79ea2090eab792c2c60a1ce24a62 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Mon, 3 Apr 2017 20:13:11 -0600 Subject: [PATCH] RefreshEventListener no longer depends on RefreshEndpoint. Instead it depends on ContextRefresher with actually does the work. This allows applications to use the listener even if they don't depend on actuator. fixes gh-171 --- spring-cloud-context/pom.xml | 6 +++ .../RefreshAutoConfiguration.java | 7 ++++ .../RefreshEndpointAutoConfiguration.java | 16 ------- .../endpoint/event/RefreshEventListener.java | 14 +++---- ...efreshAutoConfigurationClassPathTests.java | 42 +++++++++++++++++++ 5 files changed, 62 insertions(+), 23 deletions(-) create mode 100644 spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/RefreshAutoConfigurationClassPathTests.java diff --git a/spring-cloud-context/pom.xml b/spring-cloud-context/pom.xml index f668bfb2..99fe3d84 100644 --- a/spring-cloud-context/pom.xml +++ b/spring-cloud-context/pom.xml @@ -59,5 +59,11 @@ spring-boot-starter-test test + + org.springframework.cloud + spring-cloud-commons + ${project.version} + test-jar + diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshAutoConfiguration.java b/spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshAutoConfiguration.java index 6ee17ede..339569b5 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshAutoConfiguration.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshAutoConfiguration.java @@ -30,6 +30,7 @@ import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.cloud.context.environment.EnvironmentManager; import org.springframework.cloud.context.refresh.ContextRefresher; import org.springframework.cloud.context.scope.refresh.RefreshScope; +import org.springframework.cloud.endpoint.event.RefreshEventListener; import org.springframework.cloud.logging.LoggingRebinder; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; @@ -88,4 +89,10 @@ public class RefreshAutoConfiguration { return new ContextRefresher(context, scope); } + @Bean + public RefreshEventListener refreshEventListener( + ContextRefresher contextRefresher) { + return new RefreshEventListener(contextRefresher); + } + } 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 32c58d29..38fb06ce 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,16 +16,10 @@ package org.springframework.cloud.autoconfigure; -import java.util.LinkedHashMap; -import java.util.Map; - -import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.boot.actuate.autoconfigure.ConditionalOnEnabledHealthIndicator; import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration; import org.springframework.boot.actuate.endpoint.Endpoint; -import org.springframework.boot.actuate.endpoint.InfoEndpoint; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -34,18 +28,14 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClas import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration; -import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder; import org.springframework.cloud.context.refresh.ContextRefresher; import org.springframework.cloud.context.restart.RestartEndpoint; import org.springframework.cloud.context.scope.refresh.RefreshScope; import org.springframework.cloud.endpoint.RefreshEndpoint; -import org.springframework.cloud.endpoint.event.RefreshEventListener; import org.springframework.cloud.health.RefreshScopeHealthIndicator; -import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.integration.monitor.IntegrationMBeanExporter; /** @@ -119,11 +109,5 @@ public class RefreshEndpointAutoConfiguration { return endpoint; } - @Bean - public RefreshEventListener refreshEventListener( - RefreshEndpoint refreshEndpoint) { - return new RefreshEventListener(refreshEndpoint); - } - } } \ No newline at end of file diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/endpoint/event/RefreshEventListener.java b/spring-cloud-context/src/main/java/org/springframework/cloud/endpoint/event/RefreshEventListener.java index 4cda9706..16cf5e7f 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/endpoint/event/RefreshEventListener.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/endpoint/event/RefreshEventListener.java @@ -1,25 +1,25 @@ package org.springframework.cloud.endpoint.event; -import java.util.Arrays; +import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import org.springframework.boot.context.event.ApplicationReadyEvent; -import org.springframework.cloud.endpoint.RefreshEndpoint; +import org.springframework.cloud.context.refresh.ContextRefresher; import org.springframework.context.event.EventListener; import lombok.extern.apachecommons.CommonsLog; /** - * Calls {@link RefreshEndpoint#refresh()} when a {@link RefreshEvent} is received. + * Calls {@link RefreshEventListener#refresh} when a {@link RefreshEvent} is received. * Only responds to {@link RefreshEvent} after receiving an {@link ApplicationReadyEvent} as the RefreshEvent's might come to early in the application lifecycle. * @author Spencer Gibb */ @CommonsLog public class RefreshEventListener { - private RefreshEndpoint refresh; + private ContextRefresher refresh; private AtomicBoolean ready = new AtomicBoolean(false); - public RefreshEventListener(RefreshEndpoint refresh) { + public RefreshEventListener(ContextRefresher refresh) { this.refresh = refresh; } @@ -32,8 +32,8 @@ public class RefreshEventListener { public void handle(RefreshEvent event) { if (this.ready.get()) { // don't handle events before app is ready log.debug("Event received " + event.getEventDesc()); - String[] keys = this.refresh.refresh(); - log.info("Refresh keys changed: " + Arrays.asList(keys)); + Set keys = this.refresh.refresh(); + log.info("Refresh keys changed: " + keys); } } } diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/RefreshAutoConfigurationClassPathTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/RefreshAutoConfigurationClassPathTests.java new file mode 100644 index 00000000..a6b190cb --- /dev/null +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/RefreshAutoConfigurationClassPathTests.java @@ -0,0 +1,42 @@ +package org.springframework.cloud.autoconfigure; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.ClassPathExclusions; +import org.springframework.cloud.FilteredClassPathRunner; +import org.springframework.cloud.endpoint.event.RefreshEventListener; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Configuration; + +import static org.junit.Assert.assertFalse; + +/** + * @author Spencer Gibb + */ +@RunWith(FilteredClassPathRunner.class) +@ClassPathExclusions({"spring-boot-actuator-*.jar", "spring-boot-starter-actuator-*.jar"}) +public class RefreshAutoConfigurationClassPathTests { + + @Test + public void refreshEventListenerCreated() { + try (ConfigurableApplicationContext context = getApplicationContext( + Config.class)) { + assertFalse(context.getBeansOfType(RefreshEventListener.class).isEmpty()); + assertFalse(context.containsBean("refeshEndpoint")); + } + } + + private static ConfigurableApplicationContext getApplicationContext( + Class configuration, String... properties) { + return new SpringApplicationBuilder(configuration).web(false) + .properties(properties).run(); + } + + @Configuration + @EnableAutoConfiguration + static class Config { + + } +}