From b31ce01adf12ee1e6da5b9327a01866b5831d68d Mon Sep 17 00:00:00 2001 From: shedfreewu Date: Thu, 21 Nov 2024 16:02:09 +0800 Subject: [PATCH] fix review --- .../spring/annotation/SpringValueProcessor.java | 15 ++++++++++++--- .../spring/property/SpringValueRegistry.java | 5 +++++ .../RefreshScopeSpringProcessorTest.java | 4 +++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java index 0656a1069..96912ea31 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java @@ -149,6 +149,9 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanDefini /** * @RefreshScope on method. + * method parameter with @Value ${@link com.tencent.cloud.polaris.config.spring.annotation.RefreshScopeSpringProcessorTest.TestConfig3#testBean3}. + * method parameter class with @ConfigurationProperties ${@link com.tencent.cloud.polaris.config.spring.annotation.RefreshScopeSpringProcessorTest.TestConfig4#testBean4}. + * @ConfigurationProperties outside method may effect @RefreshScope bean${@link com.tencent.cloud.polaris.config.spring.annotation.RefreshScopeSpringProcessorTest.TestConfig5#testBean5()}. * @param bean spring bean. * @param method method. */ @@ -205,9 +208,15 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanDefini } /** - * parse refresh scope keys from @ConfigurationProperties and prefix. - * @param configClazz class of @ConfigurationProperties bean. - * @param prefix config prefix. + * parse all fields of the configClazz. + * if the field is primitive or wrapper, add it to refresh scope key map. + * ${@link com.tencent.cloud.polaris.config.spring.annotation.RefreshScopeSpringProcessorTest.TestBeanProperties2#name} + * if the field is collection, add it to refresh scope prefix trie node. + * ${@link com.tencent.cloud.polaris.config.spring.annotation.RefreshScopeSpringProcessorTest.TestBeanProperties2#list} + * if the field is complex type, recursive parse. + * ${@link com.tencent.cloud.polaris.config.spring.annotation.RefreshScopeSpringProcessorTest.TestBeanProperties2#inner} + * @param configClazz class or subclass of @ConfigurationProperties bean. + * @param prefix prefix or subclass's prefix of @ConfigurationProperties bean. */ private void parseConfigKeys(Class configClazz, String prefix) { for (Field field : findAllField(configClazz)) { diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueRegistry.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueRegistry.java index 1e865087c..724ea7357 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueRegistry.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueRegistry.java @@ -131,6 +131,11 @@ public class SpringValueRegistry implements DisposableBean { refreshScopeKeys.addAll(keys); } + /** + * first check if the key is in refreshScopeKeys, if not, check the key by TrieUtil. + * @param key changed key. + * @return true if the key is refresh scope key, otherwise false. + */ public boolean isRefreshScopeKey(String key) { if (refreshScopeKeys.contains(key)) { return true; diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/RefreshScopeSpringProcessorTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/RefreshScopeSpringProcessorTest.java index d4a70ab6d..7507ab6f3 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/RefreshScopeSpringProcessorTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/RefreshScopeSpringProcessorTest.java @@ -211,7 +211,9 @@ public class RefreshScopeSpringProcessorTest { @Bean @RefreshScope public TestBean testBean5() { - return new TestBean(); + TestBean testBean = new TestBean(); + testBean.setName(testBeanProperties2.getName()); + return testBean; } }