Skip to content

Commit 612188b

Browse files
fix: fix watch tsf config, fix bean refresh with RefreshScope and ConfigurationProperties. (#1512)
Co-authored-by: shedfreewu <[email protected]>
1 parent 70a32f2 commit 612188b

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@
5353
- [feat:support default instance circuit breaker rule.](https://github.com/Tencent/spring-cloud-tencent/pull/1506)
5454
- [docs:update JDK version configuration in GitHub Actions.](https://github.com/Tencent/spring-cloud-tencent/pull/1507)
5555
- [fix: fix count circuit breaker in gateway & return 404 when context api does not match.](https://github.com/Tencent/spring-cloud-tencent/pull/1509)
56+
- [fix:fix watch tsf config, fix bean refresh with RefreshScope and ConfigurationProperties.](https://github.com/Tencent/spring-cloud-tencent/pull/1512)

spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ public static PolarisPropertySource loadGroupPolarisPropertySource(ConfigFileSer
118118

119119
public static ConfigKVFile loadConfigKVFile(ConfigFileService configFileService, String namespace, String group, String fileName) {
120120
ConfigKVFile configKVFile;
121-
// unknown extension is resolved as properties file
122-
if (ConfigFileFormat.isPropertyFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) {
123-
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
124-
}
125-
else if (ConfigFileFormat.isYamlFile(fileName)) {
121+
// unknown extension is resolved as yaml file
122+
if (ConfigFileFormat.isYamlFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) {
126123
configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName);
127124
}
125+
else if (ConfigFileFormat.isPropertyFile(fileName)) {
126+
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
127+
}
128128
else {
129129
LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace, group, fileName);
130130

spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisProcessor.java

+6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public Object postProcessBeforeInitialization(Object bean, @NonNull String beanN
5858
catch (Exception ignored) {
5959
// ignore
6060
}
61+
processClass(bean, beanName, clazz, isRefreshScope);
6162

6263
for (Field field : findAllField(clazz)) {
6364
processField(bean, beanName, field, isRefreshScope);
@@ -73,6 +74,11 @@ public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull Stri
7374
return bean;
7475
}
7576

77+
/**
78+
* subclass should implement this method to process class.
79+
*/
80+
protected abstract void processClass(Object bean, String beanName, Class<?> clazz, boolean isRefreshScope);
81+
7682
/**
7783
* subclass should implement this method to process field.
7884
* @param bean bean

spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java

+9
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@ public Object postProcessBeforeInitialization(Object bean, @NonNull String beanN
135135
return bean;
136136
}
137137

138+
@Override
139+
protected void processClass(Object bean, String beanName, Class<?> clazz, boolean isRefreshScope) {
140+
ConfigurationProperties configurationProperties = clazz.getAnnotation(ConfigurationProperties.class);
141+
if (configurationProperties != null && isRefreshScope) {
142+
// A bean with RefreshScope and ConfigurationProperties can't be refreshed by reflection, because it's proxied by Spring AOP. Related keys needs to be registered
143+
parseConfigurationPropertiesKeys(configurationProperties, clazz);
144+
}
145+
}
146+
138147
@Override
139148
protected void processField(Object bean, String beanName, Field field, boolean isRefreshScope) {
140149
// register @Value on field

spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/RefreshScopeSpringProcessorTest.java

+19
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public void springValueFiledProcessorTest() {
8787
.withConfiguration(AutoConfigurations.of(TestConfig5.class))
8888
.withConfiguration(AutoConfigurations.of(TestBeanProperties1.class))
8989
.withConfiguration(AutoConfigurations.of(TestBeanProperties2.class))
90+
.withConfiguration(AutoConfigurations.of(TestBeanProperties3.class))
9091
.withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class))
9192
.withAllowBeanDefinitionOverriding(true)
9293
.withPropertyValues("spring.application.name=" + "conditionalOnConfigReflectEnabledTest")
@@ -124,6 +125,9 @@ public void springValueFiledProcessorTest() {
124125
assertThat(springValueRegistry.isRefreshScopeKey("test.properties2.map.key")).isTrue();
125126

126127
assertThat(springValueRegistry.isRefreshScopeKey("test.properties2.notExist")).isFalse();
128+
// @RefreshScope and @ConfigurationProperties on @Component bean
129+
assertThat(springValueRegistry.isRefreshScopeKey("test.properties3.name")).isTrue();
130+
assertThat(springValueRegistry.isRefreshScopeKey("test.properties3.notExist")).isFalse();
127131
// @RefreshScope and @Bean on method, @Value bean in class
128132
assertThat(springValueRegistry.isRefreshScopeKey("test.bean5.name")).isTrue();
129133
});
@@ -319,6 +323,21 @@ public void setInner(InnerProperties inner) {
319323
}
320324
}
321325

326+
@Component
327+
@RefreshScope
328+
@ConfigurationProperties(prefix = "test.properties3")
329+
static class TestBeanProperties3 {
330+
private String name;
331+
332+
public String getName() {
333+
return name;
334+
}
335+
336+
public void setName(String name) {
337+
this.name = name;
338+
}
339+
}
340+
322341
static class InnerProperties {
323342
private String name;
324343

0 commit comments

Comments
 (0)