Skip to content

fix: fix watch tsf config, fix bean refresh with RefreshScope and ConfigurationProperties #1520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
- [feat: support 2.0.0 config](https://github.com/Tencent/spring-cloud-tencent/pull/1463)
- [feat:upgrade trace plugin.](https://github.com/Tencent/spring-cloud-tencent/pull/1467)
- [feat:upgrade 2.0.0 service.](https://github.com/Tencent/spring-cloud-tencent/pull/1471)
- [fix:fix zuul delay circuit breaker.](https://github.com/Tencent/spring-cloud-tencent/pull/1519)
- [fix:fix zuul delay circuit breaker.](https://github.com/Tencent/spring-cloud-tencent/pull/1519)
- [fix:fix watch tsf config, fix bean refresh with RefreshScope and ConfigurationProperties.](https://github.com/Tencent/spring-cloud-tencent/pull/1520)
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,13 @@ public static PolarisPropertySource loadGroupPolarisPropertySource(ConfigFileSer

public static ConfigKVFile loadConfigKVFile(ConfigFileService configFileService, String namespace, String group, String fileName) {
ConfigKVFile configKVFile;
// unknown extension is resolved as properties file
if (ConfigFileFormat.isPropertyFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) {
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
}
else if (ConfigFileFormat.isYamlFile(fileName)) {
// unknown extension is resolved as yaml file
if (ConfigFileFormat.isYamlFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) {
configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName);
}
else if (ConfigFileFormat.isPropertyFile(fileName)) {
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
}
else {
LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace, group, fileName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public Object postProcessBeforeInitialization(Object bean, @NonNull String beanN
catch (Exception ignored) {
// ignore
}
processClass(bean, beanName, clazz, isRefreshScope);

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

/**
* subclass should implement this method to process class.
*/
protected abstract void processClass(Object bean, String beanName, Class<?> clazz, boolean isRefreshScope);

/**
* subclass should implement this method to process field.
* @param bean bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ public Object postProcessBeforeInitialization(Object bean, @NonNull String beanN
}


@Override
protected void processClass(Object bean, String beanName, Class<?> clazz, boolean isRefreshScope) {
ConfigurationProperties configurationProperties = clazz.getAnnotation(ConfigurationProperties.class);
if (configurationProperties != null && isRefreshScope) {
// 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
parseConfigurationPropertiesKeys(configurationProperties, clazz);
}
}

@Override
protected void processField(Object bean, String beanName, Field field, boolean isRefreshScope) {
// register @Value on field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void springValueFiledProcessorTest() {
.withConfiguration(AutoConfigurations.of(TestConfig5.class))
.withConfiguration(AutoConfigurations.of(TestBeanProperties1.class))
.withConfiguration(AutoConfigurations.of(TestBeanProperties2.class))
.withConfiguration(AutoConfigurations.of(TestBeanProperties3.class))
.withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class))
.withAllowBeanDefinitionOverriding(true)
.withPropertyValues("spring.application.name=" + "conditionalOnConfigReflectEnabledTest")
Expand Down Expand Up @@ -124,6 +125,9 @@ public void springValueFiledProcessorTest() {
assertThat(springValueRegistry.isRefreshScopeKey("test.properties2.map.key")).isTrue();

assertThat(springValueRegistry.isRefreshScopeKey("test.properties2.notExist")).isFalse();
// @RefreshScope and @ConfigurationProperties on @Component bean
assertThat(springValueRegistry.isRefreshScopeKey("test.properties3.name")).isTrue();
assertThat(springValueRegistry.isRefreshScopeKey("test.properties3.notExist")).isFalse();
// @RefreshScope and @Bean on method, @Value bean in class
assertThat(springValueRegistry.isRefreshScopeKey("test.bean5.name")).isTrue();
});
Expand Down Expand Up @@ -319,6 +323,21 @@ public void setInner(InnerProperties inner) {
}
}

@Component
@RefreshScope
@ConfigurationProperties(prefix = "test.properties3")
static class TestBeanProperties3 {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

static class InnerProperties {
private String name;

Expand Down