Skip to content

Commit b40ceef

Browse files
committed
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 spring-projectsgh-171
1 parent 78d10fc commit b40ceef

File tree

5 files changed

+62
-23
lines changed

5 files changed

+62
-23
lines changed

spring-cloud-context/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,11 @@
5959
<artifactId>spring-boot-starter-test</artifactId>
6060
<scope>test</scope>
6161
</dependency>
62+
<dependency>
63+
<groupId>org.springframework.cloud</groupId>
64+
<artifactId>spring-cloud-commons</artifactId>
65+
<version>${project.version}</version>
66+
<type>test-jar</type>
67+
</dependency>
6268
</dependencies>
6369
</project>

spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshAutoConfiguration.java

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.cloud.context.environment.EnvironmentManager;
3131
import org.springframework.cloud.context.refresh.ContextRefresher;
3232
import org.springframework.cloud.context.scope.refresh.RefreshScope;
33+
import org.springframework.cloud.endpoint.event.RefreshEventListener;
3334
import org.springframework.cloud.logging.LoggingRebinder;
3435
import org.springframework.context.ConfigurableApplicationContext;
3536
import org.springframework.context.annotation.Bean;
@@ -88,4 +89,10 @@ public ContextRefresher contextRefresher(ConfigurableApplicationContext context,
8889
return new ContextRefresher(context, scope);
8990
}
9091

92+
@Bean
93+
public RefreshEventListener refreshEventListener(
94+
ContextRefresher contextRefresher) {
95+
return new RefreshEventListener(contextRefresher);
96+
}
97+
9198
}

spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshEndpointAutoConfiguration.java

-16
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,10 @@
1616

1717
package org.springframework.cloud.autoconfigure;
1818

19-
import java.util.LinkedHashMap;
20-
import java.util.Map;
21-
22-
import org.springframework.beans.BeansException;
2319
import org.springframework.beans.factory.annotation.Autowired;
24-
import org.springframework.beans.factory.config.BeanPostProcessor;
2520
import org.springframework.boot.actuate.autoconfigure.ConditionalOnEnabledHealthIndicator;
2621
import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;
2722
import org.springframework.boot.actuate.endpoint.Endpoint;
28-
import org.springframework.boot.actuate.endpoint.InfoEndpoint;
2923
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
3024
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
3125
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -34,18 +28,14 @@
3428
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3529
import org.springframework.boot.context.properties.ConfigurationProperties;
3630
import org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration;
37-
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
3831
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder;
3932
import org.springframework.cloud.context.refresh.ContextRefresher;
4033
import org.springframework.cloud.context.restart.RestartEndpoint;
4134
import org.springframework.cloud.context.scope.refresh.RefreshScope;
4235
import org.springframework.cloud.endpoint.RefreshEndpoint;
43-
import org.springframework.cloud.endpoint.event.RefreshEventListener;
4436
import org.springframework.cloud.health.RefreshScopeHealthIndicator;
45-
import org.springframework.context.ApplicationListener;
4637
import org.springframework.context.annotation.Bean;
4738
import org.springframework.context.annotation.Configuration;
48-
import org.springframework.core.env.ConfigurableEnvironment;
4939
import org.springframework.integration.monitor.IntegrationMBeanExporter;
5040

5141
/**
@@ -119,11 +109,5 @@ public RefreshEndpoint refreshEndpoint(ContextRefresher contextRefresher) {
119109
return endpoint;
120110
}
121111

122-
@Bean
123-
public RefreshEventListener refreshEventListener(
124-
RefreshEndpoint refreshEndpoint) {
125-
return new RefreshEventListener(refreshEndpoint);
126-
}
127-
128112
}
129113
}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
package org.springframework.cloud.endpoint.event;
22

3-
import java.util.Arrays;
3+
import java.util.Set;
44
import java.util.concurrent.atomic.AtomicBoolean;
55

66
import org.springframework.boot.context.event.ApplicationReadyEvent;
7-
import org.springframework.cloud.endpoint.RefreshEndpoint;
7+
import org.springframework.cloud.context.refresh.ContextRefresher;
88
import org.springframework.context.event.EventListener;
99

1010
import lombok.extern.apachecommons.CommonsLog;
1111

1212
/**
13-
* Calls {@link RefreshEndpoint#refresh()} when a {@link RefreshEvent} is received.
13+
* Calls {@link RefreshEventListener#refresh} when a {@link RefreshEvent} is received.
1414
* Only responds to {@link RefreshEvent} after receiving an {@link ApplicationReadyEvent} as the RefreshEvent's might come to early in the application lifecycle.
1515
* @author Spencer Gibb
1616
*/
1717
@CommonsLog
1818
public class RefreshEventListener {
19-
private RefreshEndpoint refresh;
19+
private ContextRefresher refresh;
2020
private AtomicBoolean ready = new AtomicBoolean(false);
2121

22-
public RefreshEventListener(RefreshEndpoint refresh) {
22+
public RefreshEventListener(ContextRefresher refresh) {
2323
this.refresh = refresh;
2424
}
2525

@@ -32,8 +32,8 @@ public void handle(ApplicationReadyEvent event) {
3232
public void handle(RefreshEvent event) {
3333
if (this.ready.get()) { // don't handle events before app is ready
3434
log.debug("Event received " + event.getEventDesc());
35-
String[] keys = this.refresh.refresh();
36-
log.info("Refresh keys changed: " + Arrays.asList(keys));
35+
Set<String> keys = this.refresh.refresh();
36+
log.info("Refresh keys changed: " + keys);
3737
}
3838
}
3939
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.springframework.cloud.autoconfigure;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
6+
import org.springframework.boot.builder.SpringApplicationBuilder;
7+
import org.springframework.cloud.ClassPathExclusions;
8+
import org.springframework.cloud.FilteredClassPathRunner;
9+
import org.springframework.cloud.endpoint.event.RefreshEventListener;
10+
import org.springframework.context.ConfigurableApplicationContext;
11+
import org.springframework.context.annotation.Configuration;
12+
13+
import static org.junit.Assert.assertFalse;
14+
15+
/**
16+
* @author Spencer Gibb
17+
*/
18+
@RunWith(FilteredClassPathRunner.class)
19+
@ClassPathExclusions({"spring-boot-actuator-*.jar", "spring-boot-starter-actuator-*.jar"})
20+
public class RefreshAutoConfigurationClassPathTests {
21+
22+
@Test
23+
public void refreshEventListenerCreated() {
24+
try (ConfigurableApplicationContext context = getApplicationContext(
25+
Config.class)) {
26+
assertFalse(context.getBeansOfType(RefreshEventListener.class).isEmpty());
27+
assertFalse(context.containsBean("refeshEndpoint"));
28+
}
29+
}
30+
31+
private static ConfigurableApplicationContext getApplicationContext(
32+
Class<?> configuration, String... properties) {
33+
return new SpringApplicationBuilder(configuration).web(false)
34+
.properties(properties).run();
35+
}
36+
37+
@Configuration
38+
@EnableAutoConfiguration
39+
static class Config {
40+
41+
}
42+
}

0 commit comments

Comments
 (0)