From 90d6d18c1bf21bddb006eec32785cf6e0fa53c0d Mon Sep 17 00:00:00 2001 From: wulingxiao <1251605638@qqcom> Date: Fri, 5 Aug 2022 23:46:00 +0800 Subject: [PATCH] feature:add @ConditionalOnConfigReflectEnabled annotation --- .../PolarisConfigAutoConfiguration.java | 32 +++-- .../PolarisPropertySourceAutoRefresher.java | 118 +-------------- .../PolarisPropertySourceRefresher.java | 40 ++++++ ...risReflectPropertySourceAutoRefresher.java | 134 ++++++++++++++++++ ...eshContextPropertySourceAutoRefresher.java | 49 +++++++ .../ConditionalOnConfigReflectEnabled.java | 12 +- .../condition/ConfigReflectCondition.java | 53 +++++++ ...arisPropertiesSourceAutoRefresherTest.java | 10 +- ...ConditionalOnConfigReflectEnabledTest.java | 29 +++- .../metadata/StaticMetadataManagerTest.java | 2 +- 10 files changed, 338 insertions(+), 141 deletions(-) create mode 100644 spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceRefresher.java create mode 100644 spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisReflectPropertySourceAutoRefresher.java create mode 100644 spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshContextPropertySourceAutoRefresher.java rename spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/{ => condition}/ConditionalOnConfigReflectEnabled.java (80%) create mode 100644 spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ConfigReflectCondition.java rename spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/{ => condition}/ConditionalOnConfigReflectEnabledTest.java (58%) diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java index 44b4d751b..d416d6b0f 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java @@ -18,10 +18,13 @@ package com.tencent.cloud.polaris.config; -import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceAutoRefresher; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; +import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceRefresher; +import com.tencent.cloud.polaris.config.adapter.PolarisReflectPropertySourceAutoRefresher; +import com.tencent.cloud.polaris.config.adapter.PolarisRefreshContextPropertySourceAutoRefresher; import com.tencent.cloud.polaris.config.adapter.SmartConfigurationPropertiesRebinder; import com.tencent.cloud.polaris.config.annotation.PolarisConfigAnnotationProcessor; +import com.tencent.cloud.polaris.config.condition.ConditionalOnConfigReflectEnabled; import com.tencent.cloud.polaris.config.condition.ConditionalOnNonDefaultBehavior; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.listener.PolarisConfigChangeEventListener; @@ -38,7 +41,6 @@ import org.springframework.cloud.context.properties.ConfigurationPropertiesRebin import org.springframework.cloud.context.refresh.ContextRefresher; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.lang.Nullable; /** * polaris config module auto configuration at init application context phase. @@ -50,17 +52,6 @@ import org.springframework.lang.Nullable; @ConditionalOnProperty(value = "spring.cloud.polaris.config.enabled", matchIfMissing = true) public class PolarisConfigAutoConfiguration { - @Bean - public PolarisPropertySourceAutoRefresher polarisPropertySourceAutoRefresher( - PolarisConfigProperties polarisConfigProperties, - PolarisPropertySourceManager polarisPropertySourceManager, - SpringValueRegistry springValueRegistry, - PlaceholderHelper placeholderHelper, - ContextRefresher contextRefresher) { - return new PolarisPropertySourceAutoRefresher(polarisConfigProperties, - polarisPropertySourceManager, springValueRegistry, placeholderHelper, contextRefresher); - } - @Bean public PolarisConfigAnnotationProcessor polarisConfigAnnotationProcessor() { return new PolarisConfigAnnotationProcessor(); @@ -89,6 +80,15 @@ public class PolarisConfigAutoConfiguration { return new SpringValueProcessor(placeholderHelper, springValueRegistry, polarisConfigProperties); } + @Bean + @ConditionalOnConfigReflectEnabled + public PolarisPropertySourceRefresher polarisReflectPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties, + PolarisPropertySourceManager polarisPropertySourceManager, + SpringValueRegistry springValueRegistry, + PlaceholderHelper placeholderHelper) { + return new PolarisReflectPropertySourceAutoRefresher(polarisConfigProperties, polarisPropertySourceManager, springValueRegistry, placeholderHelper); + } + @Bean @ConditionalOnMissingBean(search = SearchStrategy.CURRENT) @ConditionalOnNonDefaultBehavior @@ -99,4 +99,10 @@ public class PolarisConfigAutoConfiguration { return new SmartConfigurationPropertiesRebinder(beans); } + @Bean + @ConditionalOnMissingBean(search = SearchStrategy.CURRENT) + public PolarisPropertySourceRefresher polarisRefreshContextPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties, + PolarisPropertySourceManager polarisPropertySourceManager, ContextRefresher contextRefresher) { + return new PolarisRefreshContextPropertySourceAutoRefresher(polarisConfigProperties, polarisPropertySourceManager, contextRefresher); + } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceAutoRefresher.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceAutoRefresher.java index 9aa001194..929a721f0 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceAutoRefresher.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceAutoRefresher.java @@ -18,34 +18,18 @@ package com.tencent.cloud.polaris.config.adapter; -import java.util.Collection; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; -import com.tencent.cloud.common.util.JacksonUtils; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; -import com.tencent.cloud.polaris.config.enums.RefreshType; -import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper; -import com.tencent.cloud.polaris.config.spring.property.SpringValue; -import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry; import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeListener; import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.BeansException; -import org.springframework.beans.TypeConverter; -import org.springframework.beans.factory.BeanFactory; -import org.springframework.beans.factory.BeanFactoryAware; -import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.boot.context.event.ApplicationReadyEvent; -import org.springframework.cloud.context.environment.EnvironmentChangeEvent; -import org.springframework.cloud.context.refresh.ContextRefresher; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.util.CollectionUtils; /** @@ -54,8 +38,8 @@ import org.springframework.util.CollectionUtils; * * @author lepdou 2022-03-28 */ -public class PolarisPropertySourceAutoRefresher - implements ApplicationListener, ApplicationContextAware, BeanFactoryAware { +public abstract class PolarisPropertySourceAutoRefresher + implements ApplicationListener, PolarisPropertySourceRefresher { private static final Logger LOGGER = LoggerFactory.getLogger(PolarisPropertySourceAutoRefresher.class); @@ -63,35 +47,13 @@ public class PolarisPropertySourceAutoRefresher private final PolarisPropertySourceManager polarisPropertySourceManager; - private final ContextRefresher contextRefresher; - private final AtomicBoolean registered = new AtomicBoolean(false); - private ConfigurableApplicationContext context; - - private TypeConverter typeConverter; - private final SpringValueRegistry springValueRegistry; - private ConfigurableBeanFactory beanFactory; - private final PlaceholderHelper placeholderHelper; - public PolarisPropertySourceAutoRefresher( PolarisConfigProperties polarisConfigProperties, - PolarisPropertySourceManager polarisPropertySourceManager, - SpringValueRegistry springValueRegistry, - PlaceholderHelper placeholderHelper, - ContextRefresher contextRefresher) { + PolarisPropertySourceManager polarisPropertySourceManager) { this.polarisConfigProperties = polarisConfigProperties; this.polarisPropertySourceManager = polarisPropertySourceManager; - this.springValueRegistry = springValueRegistry; - this.placeholderHelper = placeholderHelper; - this.contextRefresher = contextRefresher; - } - - @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - this.context = (ConfigurableApplicationContext) applicationContext; - this.beanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory(); - this.typeConverter = this.beanFactory.getTypeConverter(); } @Override @@ -142,78 +104,12 @@ public class PolarisPropertySourceAutoRefresher source.remove(changedKey); break; } - - if (polarisConfigProperties.getRefreshType() == RefreshType.REFLECT) { - Collection targetValues = springValueRegistry.get(beanFactory, changedKey); - if (targetValues == null || targetValues.isEmpty()) { - continue; - } - // update the attribute with @Value annotation - for (SpringValue val : targetValues) { - updateSpringValue(val); - } - } - } - - if (polarisConfigProperties.getRefreshType() == RefreshType.REFLECT) { - // update @ConfigurationProperties beans - context.publishEvent(new EnvironmentChangeEvent(context, configKVFileChangeEvent.changedKeys())); - } - else { - contextRefresher.refresh(); + // update the attribute with @Value annotation + refreshSpringValue(changedKey); } + // update @ConfigurationProperties beans + refreshConfigurationProperties(configKVFileChangeEvent.changedKeys()); }); } } - - private void updateSpringValue(SpringValue springValue) { - try { - Object value = resolvePropertyValue(springValue); - springValue.update(value); - - LOGGER.info("Auto update polaris changed value successfully, new value: {}, {}", value, - springValue); - } - catch (Throwable ex) { - LOGGER.error("Auto update polaris changed value failed, {}", springValue.toString(), ex); - } - } - - - /** - * Logic transplanted from DefaultListableBeanFactory. - * - * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#doResolveDependency(org.springframework.beans.factory.config.DependencyDescriptor, - * java.lang.String, java.util.Set, org.springframework.beans.TypeConverter) - */ - private Object resolvePropertyValue(SpringValue springValue) { - // value will never be null - Object value = placeholderHelper - .resolvePropertyValue(beanFactory, springValue.getBeanName(), springValue.getPlaceholder()); - - if (springValue.isJson()) { - value = parseJsonValue((String) value, springValue.getTargetType()); - } - else { - value = springValue.isField() ? this.typeConverter.convertIfNecessary(value, springValue.getTargetType(), springValue.getField()) : - this.typeConverter.convertIfNecessary(value, springValue.getTargetType(), - springValue.getMethodParameter()); - } - return value; - } - - private Object parseJsonValue(String json, Class targetType) { - try { - return JacksonUtils.json2JavaBean(json, targetType); - } - catch (Throwable ex) { - LOGGER.error("Parsing json '{}' to type {} failed!", json, targetType, ex); - throw ex; - } - } - - @Override - public void setBeanFactory(BeanFactory beanFactory) throws BeansException { - this.beanFactory = (ConfigurableBeanFactory) beanFactory; - } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceRefresher.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceRefresher.java new file mode 100644 index 000000000..0378d616d --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceRefresher.java @@ -0,0 +1,40 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.config.adapter; + +import java.util.Set; + +/** + * @author lingxiao.wlx + */ +public interface PolarisPropertySourceRefresher { + + /** + * refresh the attribute with @Value annotation. + * + * @param changedKey changedKey + */ + void refreshSpringValue(String changedKey); + + /** + * refresh @ConfigurationProperties beans. + * @param changeKeys changeKeys + */ + void refreshConfigurationProperties(Set changeKeys); +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisReflectPropertySourceAutoRefresher.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisReflectPropertySourceAutoRefresher.java new file mode 100644 index 000000000..9e3f2365b --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisReflectPropertySourceAutoRefresher.java @@ -0,0 +1,134 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.config.adapter; + +import java.util.Collection; +import java.util.Set; + +import com.tencent.cloud.common.util.JacksonUtils; +import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; +import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper; +import com.tencent.cloud.polaris.config.spring.property.SpringValue; +import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.beans.BeansException; +import org.springframework.beans.TypeConverter; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.cloud.context.environment.EnvironmentChangeEvent; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.ConfigurableApplicationContext; + +/** + * @author lingxiao.wlx + */ +public class PolarisReflectPropertySourceAutoRefresher extends PolarisPropertySourceAutoRefresher + implements ApplicationContextAware { + + private static final Logger LOGGER = LoggerFactory.getLogger(PolarisReflectPropertySourceAutoRefresher.class); + + private final SpringValueRegistry springValueRegistry; + + private final PlaceholderHelper placeholderHelper; + + private ConfigurableApplicationContext context; + + private ConfigurableBeanFactory beanFactory; + + private TypeConverter typeConverter; + + public PolarisReflectPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties, + PolarisPropertySourceManager polarisPropertySourceManager, SpringValueRegistry springValueRegistry, + PlaceholderHelper placeholderHelper) { + super(polarisConfigProperties, polarisPropertySourceManager); + this.springValueRegistry = springValueRegistry; + this.placeholderHelper = placeholderHelper; + } + + @Override + public void refreshSpringValue(String changedKey) { + Collection targetValues = springValueRegistry.get(beanFactory, changedKey); + if (targetValues == null || targetValues.isEmpty()) { + return; + } + // update the attribute with @Value annotation + for (SpringValue val : targetValues) { + updateSpringValue(val); + } + } + + @Override + public void refreshConfigurationProperties(Set changeKeys) { + context.publishEvent(new EnvironmentChangeEvent(context, changeKeys)); + } + + private void updateSpringValue(SpringValue springValue) { + try { + Object value = resolvePropertyValue(springValue); + springValue.update(value); + + LOGGER.info("Auto update polaris changed value successfully, new value: {}, {}", value, + springValue); + } + catch (Throwable ex) { + LOGGER.error("Auto update polaris changed value failed, {}", springValue.toString(), ex); + } + } + + /** + * Logic transplanted from DefaultListableBeanFactory. + * + * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#doResolveDependency(org.springframework.beans.factory.config.DependencyDescriptor, + * java.lang.String, java.util.Set, org.springframework.beans.TypeConverter) + */ + private Object resolvePropertyValue(SpringValue springValue) { + // value will never be null + Object value = placeholderHelper + .resolvePropertyValue(beanFactory, springValue.getBeanName(), springValue.getPlaceholder()); + + if (springValue.isJson()) { + value = parseJsonValue((String) value, springValue.getTargetType()); + } + else { + value = springValue.isField() ? this.typeConverter.convertIfNecessary(value, springValue.getTargetType(), springValue.getField()) : + this.typeConverter.convertIfNecessary(value, springValue.getTargetType(), + springValue.getMethodParameter()); + } + return value; + } + + private Object parseJsonValue(String json, Class targetType) { + try { + return JacksonUtils.json2JavaBean(json, targetType); + } + catch (Throwable ex) { + LOGGER.error("Parsing json '{}' to type {} failed!", json, targetType, ex); + throw ex; + } + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.context = (ConfigurableApplicationContext) applicationContext; + this.beanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory(); + this.typeConverter = this.beanFactory.getTypeConverter(); + } +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshContextPropertySourceAutoRefresher.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshContextPropertySourceAutoRefresher.java new file mode 100644 index 000000000..b1b74c616 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshContextPropertySourceAutoRefresher.java @@ -0,0 +1,49 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.config.adapter; + +import java.util.Set; + +import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; + +import org.springframework.cloud.context.refresh.ContextRefresher; + +/** + * @author lingxiao.wlx + */ +public class PolarisRefreshContextPropertySourceAutoRefresher extends PolarisPropertySourceAutoRefresher { + + private final ContextRefresher contextRefresher; + + public PolarisRefreshContextPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties, + PolarisPropertySourceManager polarisPropertySourceManager, + ContextRefresher contextRefresher) { + super(polarisConfigProperties, polarisPropertySourceManager); + this.contextRefresher = contextRefresher; + } + + @Override + public void refreshSpringValue(String changedKey) { + } + + @Override + public void refreshConfigurationProperties(Set changeKeys) { + contextRefresher.refresh(); + } +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConditionalOnConfigReflectEnabled.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ConditionalOnConfigReflectEnabled.java similarity index 80% rename from spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConditionalOnConfigReflectEnabled.java rename to spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ConditionalOnConfigReflectEnabled.java index 269f250f8..05c80dfa0 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConditionalOnConfigReflectEnabled.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ConditionalOnConfigReflectEnabled.java @@ -16,22 +16,22 @@ * */ -package com.tencent.cloud.polaris.config; - -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +package com.tencent.cloud.polaris.config.condition; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.context.annotation.Conditional; + /** - * When the refresh type is reflect, load the beans required by the reflect mode + * When the refresh type is reflect, load the beans required by the reflect mode. * * @author lingxiao.wlx */ @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.METHOD}) -@ConditionalOnProperty(value = "spring.cloud.polaris.config.refresh-type", havingValue = "reflect") +@Target({ElementType.TYPE, ElementType.METHOD}) +@Conditional(ConfigReflectCondition.class) public @interface ConditionalOnConfigReflectEnabled { } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ConfigReflectCondition.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ConfigReflectCondition.java new file mode 100644 index 000000000..0ac9986a3 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ConfigReflectCondition.java @@ -0,0 +1,53 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.config.condition; + +import com.tencent.cloud.polaris.config.enums.RefreshType; + +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; + +/** + * @author lingxiao.wlx + */ +public class ConfigReflectCondition extends SpringBootCondition { + + /** + * Refresh type config. + */ + public static final String POLARIS_CONFIG_REFRESH_TYPE = "spring.cloud.polaris.config.refresh-type"; + + /** + * Refresh type default value. + */ + private static final RefreshType DEFAULT_REFRESH_TYPE = RefreshType.REFRESH_CONTEXT; + + @Override + public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { + RefreshType refreshType = context.getEnvironment().getProperty( + POLARIS_CONFIG_REFRESH_TYPE, RefreshType.class, + DEFAULT_REFRESH_TYPE); + if (DEFAULT_REFRESH_TYPE == refreshType) { + return ConditionOutcome.noMatch("no matched"); + } + return ConditionOutcome.match("matched"); + } +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java index 03945edc1..02b3506af 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java @@ -41,7 +41,6 @@ import org.mockito.junit.MockitoJUnitRunner; import org.springframework.beans.TypeConverter; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.cloud.context.refresh.ContextRefresher; import org.springframework.context.ConfigurableApplicationContext; import static org.mockito.ArgumentMatchers.any; @@ -49,7 +48,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** - * test for {@link PolarisPropertySourceAutoRefresher}. + * test for {@link PolarisReflectPropertySourceAutoRefresher}. * * @author lepdou 2022-06-11 */ @@ -63,8 +62,6 @@ public class PolarisPropertiesSourceAutoRefresherTest { private PolarisConfigProperties polarisConfigProperties; @Mock private PolarisPropertySourceManager polarisPropertySourceManager; - @Mock - private ContextRefresher contextRefresher; @Mock private SpringValueRegistry springValueRegistry; @@ -74,9 +71,8 @@ public class PolarisPropertiesSourceAutoRefresherTest { @Test public void testConfigFileChanged() throws Exception { - PolarisPropertySourceAutoRefresher refresher = new PolarisPropertySourceAutoRefresher(polarisConfigProperties, - polarisPropertySourceManager, springValueRegistry, placeholderHelper, contextRefresher); - + PolarisReflectPropertySourceAutoRefresher refresher = new PolarisReflectPropertySourceAutoRefresher(polarisConfigProperties, + polarisPropertySourceManager, springValueRegistry, placeholderHelper); when(polarisConfigProperties.getRefreshType()).thenReturn(RefreshType.REFLECT); ConfigurableApplicationContext applicationContext = mock(ConfigurableApplicationContext.class); ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class); diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/ConditionalOnConfigReflectEnabledTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnConfigReflectEnabledTest.java similarity index 58% rename from spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/ConditionalOnConfigReflectEnabledTest.java rename to spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnConfigReflectEnabledTest.java index 730262668..b236af835 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/ConditionalOnConfigReflectEnabledTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnConfigReflectEnabledTest.java @@ -16,15 +16,23 @@ * */ -package com.tencent.cloud.polaris.config; +package com.tencent.cloud.polaris.config.condition; +import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration; +import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration; +import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; +import com.tencent.cloud.polaris.config.adapter.PolarisReflectPropertySourceAutoRefresher; +import com.tencent.cloud.polaris.config.adapter.PolarisRefreshContextPropertySourceAutoRefresher; +import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.spring.annotation.SpringValueProcessor; import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper; import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry; -import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import org.junit.jupiter.api.Test; + import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; +import org.springframework.cloud.context.refresh.ContextRefresher; import static org.assertj.core.api.Assertions.assertThat; @@ -36,18 +44,33 @@ import static org.assertj.core.api.Assertions.assertThat; public class ConditionalOnConfigReflectEnabledTest { private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class)) .withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class)) + .withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class)) .withPropertyValues("spring.application.name=" + "conditionalOnConfigReflectEnabledTest") .withPropertyValues("server.port=" + 8080) .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081"); @Test - public void testReflectEnable(){ + public void testReflectEnable() { contextRunner.withPropertyValues("spring.cloud.polaris.config.refresh-type=reflect"); + contextRunner.withPropertyValues("spring.cloud.polaris.config.enabled=true"); this.contextRunner.run(context -> { assertThat(context).hasSingleBean(PlaceholderHelper.class); assertThat(context).hasSingleBean(SpringValueRegistry.class); assertThat(context).hasSingleBean(SpringValueProcessor.class); + assertThat(context).hasSingleBean(PolarisReflectPropertySourceAutoRefresher.class); + }); + } + + @Test + public void testWithoutReflectEnable() { + contextRunner.withPropertyValues("spring.cloud.polaris.config.enabled=true"); + this.contextRunner.run(context -> { + assertThat(context).hasSingleBean(PolarisConfigProperties.class); + assertThat(context).hasSingleBean(PolarisPropertySourceManager.class); + assertThat(context).hasSingleBean(ContextRefresher.class); + assertThat(context).hasSingleBean(PolarisRefreshContextPropertySourceAutoRefresher.class); }); } } diff --git a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/StaticMetadataManagerTest.java b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/StaticMetadataManagerTest.java index b2aa08cd4..28e7b4a2f 100644 --- a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/StaticMetadataManagerTest.java +++ b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/StaticMetadataManagerTest.java @@ -38,7 +38,7 @@ import static org.mockito.Mockito.when; /** - * test for {@link StaticMetadataManager} + * test for {@link StaticMetadataManager}. *@author lepdou 2022-06-27 */ @RunWith(MockitoJUnitRunner.class)