Skip to content

Commit f0d1aab

Browse files
author
Joey Yang
committed
pump version, public ConfigurationChangedEvent with diff props
1 parent d13a9e3 commit f0d1aab

File tree

8 files changed

+60
-24
lines changed

8 files changed

+60
-24
lines changed

README-zh.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ Maven
4141

4242
<dependency>
4343
<groupId>top.code2life</groupId>
44-
<artifactId>spring-boot-dynamic-config</artifactId>
45-
<version>1.0.6</version>
44+
<artifactId>spring-boot-dynamic-config</artifactId>
45+
<version>1.0.7</version>
4646
</dependency>
4747
```
4848

4949
Gradle
5050

5151
```groovy
52-
implementation 'top.code2life:spring-boot-dynamic-config:1.0.6'
52+
implementation 'top.code2life:spring-boot-dynamic-config:1.0.7'
5353
```
5454

5555
### 步骤二:在代码中添加 @DynamicConfig 注解

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ Maven
4242
<dependency>
4343
<groupId>top.code2life</groupId>
4444
<artifactId>spring-boot-dynamic-config</artifactId>
45-
<version>1.0.6</version>
45+
<version>1.0.7</version>
4646
</dependency>
4747
```
4848

4949
Gradle
5050

5151
```groovy
52-
implementation 'top.code2life:spring-boot-dynamic-config:1.0.6'
52+
implementation 'top.code2life:spring-boot-dynamic-config:1.0.7'
5353
```
5454

5555
### Step2. Add @DynamicConfig Annotation

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ java {
1616
}
1717

1818
group = 'top.code2life'
19-
version = '1.0.6'
19+
version = '1.0.7'
2020
sourceCompatibility = JavaVersion.VERSION_1_8
2121

2222
repositories {

example/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ dependencies {
2727
annotationProcessor 'org.projectlombok:lombok:1.18.20'
2828
implementation 'org.springframework.boot:spring-boot-starter-web'
2929

30-
implementation 'top.code2life:spring-boot-dynamic-config:1.0.6'
30+
implementation 'top.code2life:spring-boot-dynamic-config:1.0.7'
3131
}

src/main/java/top/code2life/config/ConfigurationChangedEvent.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import lombok.Getter;
44
import lombok.Setter;
55
import org.springframework.context.ApplicationEvent;
6-
import org.springframework.core.env.PropertySource;
6+
7+
import java.util.Map;
78

89
/**
910
* Application event that represents configuration file has been changed
@@ -16,18 +17,12 @@
1617
public class ConfigurationChangedEvent extends ApplicationEvent {
1718

1819
/**
19-
* previous property source of changed config file
20-
*/
21-
private PropertySource<?> previous;
22-
23-
/**
24-
* current property source of changed config file
20+
* The diff properties, keys are normalized, values are newest values
2521
*/
26-
private PropertySource<?> current;
22+
private Map<Object, Object> diff;
2723

28-
ConfigurationChangedEvent(String path, PropertySource<?> previous, PropertySource<?> current) {
29-
super(path);
30-
this.previous = previous;
31-
this.current = current;
24+
ConfigurationChangedEvent(Map<Object, Object> diff) {
25+
super(diff.keySet());
26+
this.diff = diff;
3227
}
3328
}

src/main/java/top/code2life/config/ConfigurationChangedEventHandler.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor;
1212
import org.springframework.boot.origin.OriginTrackedValue;
1313
import org.springframework.context.ApplicationContext;
14+
import org.springframework.context.ApplicationEventPublisher;
1415
import org.springframework.context.event.EventListener;
16+
import org.springframework.core.Ordered;
17+
import org.springframework.core.annotation.Order;
1518
import org.springframework.util.StringUtils;
1619

1720
import java.lang.reflect.Field;
@@ -38,8 +41,11 @@ public class ConfigurationChangedEventHandler {
3841
private final BeanExpressionContext exprContext;
3942
private final ConfigurationPropertiesBindingPostProcessor processor;
4043
private final ConfigurableListableBeanFactory beanFactory;
44+
private final ApplicationEventPublisher eventPublisher;
4145

42-
ConfigurationChangedEventHandler(ApplicationContext applicationContext, BeanFactory beanFactory) {
46+
ConfigurationChangedEventHandler(ApplicationContext applicationContext, BeanFactory beanFactory,
47+
ApplicationEventPublisher eventPublisher) {
48+
this.eventPublisher = eventPublisher;
4349
if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
4450
throw new IllegalArgumentException(
4551
"DynamicConfig requires a ConfigurableListableBeanFactory");
@@ -56,9 +62,10 @@ public class ConfigurationChangedEventHandler {
5662
*
5763
* @param event ConfigurationChangedEvent indicates a configuration file changed event
5864
*/
59-
@EventListener
6065
@SuppressWarnings("unchecked")
61-
public synchronized void handleEvent(ConfigurationChangedEvent event) {
66+
@Order(Ordered.HIGHEST_PRECEDENCE)
67+
@EventListener(ConfigurationFileChangedEvent.class)
68+
public synchronized void handleEvent(ConfigurationFileChangedEvent event) {
6269
try {
6370
Map<Object, Object> diff = getPropertyDiff((Map<Object, OriginTrackedValue>) event.getPrevious().getSource(), (Map<Object, OriginTrackedValue>) event.getCurrent().getSource());
6471
Map<String, ValueBeanFieldBinder> toRefreshProps = new HashMap<>(4);
@@ -69,6 +76,7 @@ public synchronized void handleEvent(ConfigurationChangedEvent event) {
6976
}
7077
rebindRelatedConfigurationPropsBeans(diff, toRefreshProps);
7178
log.info("config changes of {} have been processed", event.getSource());
79+
eventPublisher.publishEvent(new ConfigurationChangedEvent(diff));
7280
} catch (Exception ex) {
7381
log.warn("config changes of {} can not be processed, error:", event.getSource(), ex);
7482
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package top.code2life.config;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import org.springframework.context.ApplicationEvent;
6+
import org.springframework.core.env.PropertySource;
7+
8+
/**
9+
* Application event that represents configuration file has been changed
10+
*
11+
* @author Code2Life
12+
* @see DynamicConfigPropertiesWatcher
13+
*/
14+
@Getter
15+
@Setter
16+
public class ConfigurationFileChangedEvent extends ApplicationEvent {
17+
18+
/**
19+
* previous property source of changed config file
20+
*/
21+
private PropertySource<?> previous;
22+
23+
/**
24+
* current property source of changed config file
25+
*/
26+
private PropertySource<?> current;
27+
28+
ConfigurationFileChangedEvent(String path, PropertySource<?> previous, PropertySource<?> current) {
29+
super(path);
30+
this.previous = previous;
31+
this.current = current;
32+
}
33+
}

src/main/java/top/code2life/config/DynamicConfigPropertiesWatcher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* listening any changes on configuration files, then publish ConfigurationChangedEvent.
3535
*
3636
* @author Code2Life
37-
* @see ConfigurationChangedEvent
37+
* @see ConfigurationFileChangedEvent
3838
*/
3939
@Slf4j
4040
@ConditionalOnProperty("spring.config.location")
@@ -230,7 +230,7 @@ private void loadPropertiesAndPublishEvent(PropertySourceMeta propertySourceMeta
230230
return;
231231
}
232232
PropertySource<?> newProps = newPropsList.get(0);
233-
ConfigurationChangedEvent event = new ConfigurationChangedEvent(path, env.getPropertySources().get(propertySourceName), newProps);
233+
ConfigurationFileChangedEvent event = new ConfigurationFileChangedEvent(path, env.getPropertySources().get(propertySourceName), newProps);
234234
env.getPropertySources().replace(propertySourceName, newProps);
235235
propertySourceMeta.setLastModifyTime(modifyTime);
236236
eventPublisher.publishEvent(event);

0 commit comments

Comments
 (0)