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 7708e8b0f..6d39785cf 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 @@ -29,16 +29,20 @@ 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.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.util.CollectionUtils; /** @@ -48,22 +52,21 @@ import org.springframework.util.CollectionUtils; * @author lepdou 2022-03-28 */ public class PolarisPropertySourceAutoRefresher - implements ApplicationListener, ApplicationContextAware { + implements ApplicationListener, ApplicationContextAware, BeanFactoryAware { private static final Logger LOGGER = LoggerFactory.getLogger(PolarisPropertySourceAutoRefresher.class); private final PolarisConfigProperties polarisConfigProperties; private final PolarisPropertySourceManager polarisPropertySourceManager; - + private final AtomicBoolean registered = new AtomicBoolean(false); private TypeConverter typeConverter; private final SpringValueRegistry springValueRegistry; private ConfigurableBeanFactory beanFactory; private final PlaceholderHelper placeholderHelper; - - private ApplicationContext applicationContext; + public PolarisPropertySourceAutoRefresher( PolarisConfigProperties polarisConfigProperties, @@ -78,7 +81,8 @@ public class PolarisPropertySourceAutoRefresher @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - this.applicationContext = applicationContext; + this.beanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory(); + this.typeConverter = this.beanFactory.getTypeConverter(); } @Override @@ -104,6 +108,7 @@ public class PolarisPropertySourceAutoRefresher for (PolarisPropertySource polarisPropertySource : polarisPropertySources) { polarisPropertySource.getConfigKVFile() .addChangeListener((ConfigKVFileChangeListener) configKVFileChangeEvent -> { + LOGGER.info( "[SCT Config] received polaris config change event and will refresh spring context." + "namespace = {}, group = {}, fileName = {}", @@ -114,6 +119,21 @@ public class PolarisPropertySourceAutoRefresher Map source = polarisPropertySource.getSource(); for (String changedKey : configKVFileChangeEvent.changedKeys()) { + ConfigPropertyChangeInfo configPropertyChangeInfo = configKVFileChangeEvent + .getChangeInfo(changedKey); + + LOGGER.info("[SCT Config] changed property = {}", configPropertyChangeInfo); + + switch (configPropertyChangeInfo.getChangeType()) { + case MODIFIED: + case ADDED: + source.put(changedKey, configPropertyChangeInfo.getNewValue()); + break; + case DELETED: + source.remove(changedKey); + break; + } + Collection targetValues = springValueRegistry.get(beanFactory, changedKey); if (targetValues == null || targetValues.isEmpty()) { continue; @@ -122,6 +142,7 @@ public class PolarisPropertySourceAutoRefresher for (SpringValue val : targetValues) { updateSpringValue(val); } + } }); } @@ -172,8 +193,9 @@ public class PolarisPropertySourceAutoRefresher 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/config/ConfigPropertySource.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/ConfigPropertySource.java deleted file mode 100644 index 3de48c614..000000000 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/ConfigPropertySource.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.tencent.cloud.polaris.config.config; - -import java.util.Set; - -import com.tencent.cloud.polaris.config.listener.ConfigChangeListener; - -/** - *@author : wh - *@date : 2022/7/10 23:10 - *@description: - */ -public class ConfigPropertySource extends EnumerablePropertySource { - private static final String[] EMPTY_ARRAY = new String[0]; - - ConfigPropertySource(String name, Config source) { - super(name, source); - } - - @Override - public boolean containsProperty(String name) { - return this.source.getProperty(name, null) != null; - } - - @Override - public String[] getPropertyNames() { - Set propertyNames = this.source.getPropertyNames(); - if (propertyNames.isEmpty()) { - return EMPTY_ARRAY; - } - return propertyNames.toArray(new String[propertyNames.size()]); - } - - @Override - public Object getProperty(String name) { - return this.source.getProperty(name, null); - } - - public void addChangeListener(ConfigChangeListener listener) { - this.source.addChangeListener(listener); - } -} diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/ConfigPropertySourceFactory.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/ConfigPropertySourceFactory.java deleted file mode 100644 index fc2531b82..000000000 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/ConfigPropertySourceFactory.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.tencent.cloud.polaris.config.config; - -import java.util.List; - -import com.google.common.collect.Lists; - -/** - *@author : wh - *@date : 2022/7/10 23:10 - *@description: - */ -public class ConfigPropertySourceFactory { - - private final List configPropertySources = Lists.newLinkedList(); - - public ConfigPropertySource getConfigPropertySource(String name, Config source) { - ConfigPropertySource configPropertySource = new ConfigPropertySource(name, source); - - configPropertySources.add(configPropertySource); - - return configPropertySource; - } - - public List getAllConfigPropertySources() { - return Lists.newLinkedList(configPropertySources); - } -} diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PropertySourcesProcessor.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PropertySourcesProcessor.java deleted file mode 100644 index eefc72b5e..000000000 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PropertySourcesProcessor.java +++ /dev/null @@ -1,158 +0,0 @@ -package com.tencent.cloud.polaris.config.config; - -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - -import com.google.common.collect.ImmutableSortedSet; -import com.google.common.collect.LinkedHashMultimap; -import com.google.common.collect.Multimap; -import com.google.common.collect.Sets; -import com.tencent.cloud.polaris.config.listener.ConfigChangeListener; - -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.BeanFactory; -import org.springframework.beans.factory.config.BeanFactoryPostProcessor; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.context.ApplicationEventPublisher; -import org.springframework.context.ApplicationEventPublisherAware; -import org.springframework.context.EnvironmentAware; -import org.springframework.core.Ordered; -import org.springframework.core.PriorityOrdered; -import org.springframework.core.env.CompositePropertySource; -import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.core.env.Environment; -import org.springframework.core.env.MutablePropertySources; -import org.springframework.core.env.PropertySource; -import org.springframework.core.env.StandardEnvironment; - -/** - *@author : wh - *@date : 2022/7/10 23:09 - *@description: - */ -public class PropertySourcesProcessor implements BeanFactoryPostProcessor, EnvironmentAware, - ApplicationEventPublisherAware, PriorityOrdered { - private static final Multimap NAMESPACE_NAMES = LinkedHashMultimap.create(); - private static final Set AUTO_UPDATE_INITIALIZED_BEAN_FACTORIES = Sets.newConcurrentHashSet(); - - private final ConfigPropertySourceFactory configPropertySourceFactory = SpringInjector - .getInstance(ConfigPropertySourceFactory.class); - private ConfigUtil configUtil; - private ConfigurableEnvironment environment; - private ApplicationEventPublisher applicationEventPublisher; - - public static boolean addNamespaces(Collection namespaces, int order) { - return NAMESPACE_NAMES.putAll(order, namespaces); - } - - @Override - public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { - this.configUtil = ApolloInjector.getInstance(ConfigUtil.class); - initializePropertySources(); - initializeAutoUpdatePropertiesFeature(beanFactory); - } - - private void initializePropertySources() { - if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME)) { - //already initialized - return; - } - CompositePropertySource composite; - if (configUtil.isPropertyNamesCacheEnabled()) { - composite = new CachedCompositePropertySource(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME); - } else { - composite = new CompositePropertySource(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME); - } - - //sort by order asc - ImmutableSortedSet orders = ImmutableSortedSet.copyOf(NAMESPACE_NAMES.keySet()); - Iterator iterator = orders.iterator(); - - while (iterator.hasNext()) { - int order = iterator.next(); - for (String namespace : NAMESPACE_NAMES.get(order)) { - Config config = ConfigService.getConfig(namespace); - - composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config)); - } - } - - // clean up - NAMESPACE_NAMES.clear(); - - // add after the bootstrap property source or to the first - if (environment.getPropertySources() - .contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) { - - if (configUtil.isOverrideSystemProperties()) { - // ensure ApolloBootstrapPropertySources is still the first - ensureBootstrapPropertyPrecedence(environment); - } - - environment.getPropertySources() - .addAfter(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME, composite); - } else { - if (!configUtil.isOverrideSystemProperties()) { - if (environment.getPropertySources().contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) { - environment.getPropertySources().addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, composite); - return; - } - } - environment.getPropertySources().addFirst(composite); - } - } - - private void ensureBootstrapPropertyPrecedence(ConfigurableEnvironment environment) { - MutablePropertySources propertySources = environment.getPropertySources(); - - PropertySource bootstrapPropertySource = propertySources - .get(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME); - - // not exists or already in the first place - if (bootstrapPropertySource == null || propertySources.precedenceOf(bootstrapPropertySource) == 0) { - return; - } - - propertySources.remove(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME); - propertySources.addFirst(bootstrapPropertySource); - } - - private void initializeAutoUpdatePropertiesFeature(ConfigurableListableBeanFactory beanFactory) { - if (!AUTO_UPDATE_INITIALIZED_BEAN_FACTORIES.add(beanFactory)) { - return; - } - - ConfigChangeListener configChangeEventPublisher = changeEvent -> - applicationEventPublisher.publishEvent(new ApolloConfigChangeEvent(changeEvent)); - - List configPropertySources = configPropertySourceFactory.getAllConfigPropertySources(); - for (ConfigPropertySource configPropertySource : configPropertySources) { - configPropertySource.addChangeListener(configChangeEventPublisher); - } - } - - @Override - public void setEnvironment(Environment environment) { - //it is safe enough to cast as all known environment is derived from ConfigurableEnvironment - this.environment = (ConfigurableEnvironment) environment; - } - - @Override - public int getOrder() { - //make it as early as possible - return Ordered.HIGHEST_PRECEDENCE; - } - - // for test only - static void reset() { - NAMESPACE_NAMES.clear(); - AUTO_UPDATE_INITIALIZED_BEAN_FACTORIES.clear(); - } - - @Override - public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { - this.applicationEventPublisher = applicationEventPublisher; - } -} diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisAnnotationProcessor.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisAnnotationProcessor.java deleted file mode 100644 index c0de10db9..000000000 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisAnnotationProcessor.java +++ /dev/null @@ -1,223 +0,0 @@ -/* -package com.tencent.cloud.polaris.config.spring.annotation; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.Type; -import java.util.Set; - -import com.google.common.base.Preconditions; -import com.google.common.collect.Sets; -import com.google.gson.Gson; -import com.tencent.cloud.polaris.config.listener.ConfigChangeEvent; -import com.tencent.cloud.polaris.config.listener.ConfigChangeListener; -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.factory.BeanFactory; -import org.springframework.beans.factory.BeanFactoryAware; -import org.springframework.beans.factory.config.ConfigurableBeanFactory; -import org.springframework.context.EnvironmentAware; -import org.springframework.core.annotation.AnnotationUtils; -import org.springframework.core.env.Environment; -import org.springframework.util.ReflectionUtils; - -*/ -/** - *@author : wh - *@date : 2022/7/10 14:38 - *@description: - *//* - -public class PolarisAnnotationProcessor extends PolarisProcessor implements BeanFactoryAware, - EnvironmentAware { - - private static final Logger logger = LoggerFactory.getLogger(PolarisAnnotationProcessor.class); - private static final Gson GSON = new Gson(); - - private final ConfigUtil configUtil; - private final PlaceholderHelper placeholderHelper; - private final SpringValueRegistry springValueRegistry; - - */ -/** - * resolve the expression. - *//* - - private ConfigurableBeanFactory configurableBeanFactory; - - private Environment environment; - - public PolarisAnnotationProcessor(PlaceholderHelper placeholderHelper, - SpringValueRegistry springValueRegistry) { - - configUtil = ApolloInjector.getInstance(ConfigUtil.class); - this.placeholderHelper = placeholderHelper; - this.springValueRegistry = springValueRegistry; - } - - @Override - protected void processField(Object bean, String beanName, Field field) { - this.processApolloConfig(bean, field); - this.processApolloJsonValue(bean, beanName, field); - } - - @Override - protected void processMethod(final Object bean, String beanName, final Method method) { - this.processApolloConfigChangeListener(bean, method); - this.processApolloJsonValue(bean, beanName, method); - } - - private void processApolloConfig(Object bean, Field field) { - ApolloConfig annotation = AnnotationUtils.getAnnotation(field, ApolloConfig.class); - if (annotation == null) { - return; - } - - Preconditions.checkArgument(Config.class.isAssignableFrom(field.getType()), - "Invalid type: %s for field: %s, should be Config", field.getType(), field); - - final String namespace = annotation.value(); - final String resolvedNamespace = this.environment.resolveRequiredPlaceholders(namespace); - Config config = ConfigService.getConfig(resolvedNamespace); - - ReflectionUtils.makeAccessible(field); - ReflectionUtils.setField(field, bean, config); - } - - private void processApolloConfigChangeListener(final Object bean, final Method method) { - ApolloConfigChangeListener annotation = AnnotationUtils - .findAnnotation(method, ApolloConfigChangeListener.class); - if (annotation == null) { - return; - } - Class[] parameterTypes = method.getParameterTypes(); - Preconditions.checkArgument(parameterTypes.length == 1, - "Invalid number of parameters: %s for method: %s, should be 1", parameterTypes.length, - method); - Preconditions.checkArgument(ConfigChangeEvent.class.isAssignableFrom(parameterTypes[0]), - "Invalid parameter type: %s for method: %s, should be ConfigChangeEvent", parameterTypes[0], - method); - - ReflectionUtils.makeAccessible(method); - String[] namespaces = annotation.value(); - String[] annotatedInterestedKeys = annotation.interestedKeys(); - String[] annotatedInterestedKeyPrefixes = annotation.interestedKeyPrefixes(); - ConfigChangeListener configChangeListener = new ConfigChangeListener() { - @Override - public void onChange(ConfigChangeEvent changeEvent) { - ReflectionUtils.invokeMethod(method, bean, changeEvent); - } - }; - - Set interestedKeys = - annotatedInterestedKeys.length > 0 ? Sets.newHashSet(annotatedInterestedKeys) : null; - Set interestedKeyPrefixes = - annotatedInterestedKeyPrefixes.length > 0 ? Sets.newHashSet(annotatedInterestedKeyPrefixes) - : null; - - for (String namespace : namespaces) { - final String resolvedNamespace = this.environment.resolveRequiredPlaceholders(namespace); - Config config = ConfigService.getConfig(resolvedNamespace); - - if (interestedKeys == null && interestedKeyPrefixes == null) { - config.addChangeListener(configChangeListener); - } - else { - config.addChangeListener(configChangeListener, interestedKeys, interestedKeyPrefixes); - } - } - } - - - private void processApolloJsonValue(Object bean, String beanName, Field field) { - ApolloJsonValue apolloJsonValue = AnnotationUtils.getAnnotation(field, ApolloJsonValue.class); - if (apolloJsonValue == null) { - return; - } - String placeholder = apolloJsonValue.value(); - Object propertyValue = placeholderHelper - .resolvePropertyValue(this.configurableBeanFactory, beanName, placeholder); - - // propertyValue will never be null, as @ApolloJsonValue will not allow that - if (!(propertyValue instanceof String)) { - return; - } - - boolean accessible = field.isAccessible(); - field.setAccessible(true); - ReflectionUtils - .setField(field, bean, parseJsonValue((String) propertyValue, field.getGenericType())); - field.setAccessible(accessible); - - if (configUtil.isAutoUpdateInjectedSpringPropertiesEnabled()) { - Set keys = placeholderHelper.extractPlaceholderKeys(placeholder); - for (String key : keys) { - SpringValue springValue = new SpringValue(key, placeholder, bean, beanName, field, true); - springValueRegistry.register(this.configurableBeanFactory, key, springValue); - logger.debug("Monitoring {}", springValue); - } - } - } - - private void processApolloJsonValue(Object bean, String beanName, Method method) { - ApolloJsonValue apolloJsonValue = AnnotationUtils.getAnnotation(method, ApolloJsonValue.class); - if (apolloJsonValue == null) { - return; - } - String placeHolder = apolloJsonValue.value(); - - Object propertyValue = placeholderHelper - .resolvePropertyValue(this.configurableBeanFactory, beanName, placeHolder); - - // propertyValue will never be null, as @ApolloJsonValue will not allow that - if (!(propertyValue instanceof String)) { - return; - } - - Type[] types = method.getGenericParameterTypes(); - Preconditions.checkArgument(types.length == 1, - "Ignore @Value setter {}.{}, expecting 1 parameter, actual {} parameters", - bean.getClass().getName(), method.getName(), method.getParameterTypes().length); - - boolean accessible = method.isAccessible(); - method.setAccessible(true); - ReflectionUtils.invokeMethod(method, bean, parseJsonValue((String) propertyValue, types[0])); - method.setAccessible(accessible); - - if (configUtil.isAutoUpdateInjectedSpringPropertiesEnabled()) { - Set keys = placeholderHelper.extractPlaceholderKeys(placeHolder); - for (String key : keys) { - SpringValue springValue = new SpringValue(key, apolloJsonValue.value(), bean, beanName, - method, true); - springValueRegistry.register(this.configurableBeanFactory, key, springValue); - logger.debug("Monitoring {}", springValue); - } - } - } - - private Object parseJsonValue(String json, Type targetType) { - try { - return GSON.fromJson(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.configurableBeanFactory = (ConfigurableBeanFactory) beanFactory; - } - - @Override - public void setEnvironment(Environment environment) { - this.environment = environment; - } -} -*/ diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisConfig.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisConfig.java deleted file mode 100644 index 921e9d59b..000000000 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisConfig.java +++ /dev/null @@ -1,13 +0,0 @@ -/* -package com.tencent.cloud.polaris.config.spring.annotation; - -*/ -/** - *@author : wh - *@date : 2022/7/10 14:38 - *@description: - *//* - -public class PolarisConfig { -} -*/ diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisProcessor.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisProcessor.java index bfdd9db6a..2b95379c4 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisProcessor.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisProcessor.java @@ -12,9 +12,9 @@ import org.springframework.core.PriorityOrdered; import org.springframework.util.ReflectionUtils; /** - *@author : wh - *@date : 2022/7/10 14:35 - *@description: + * Get spring bean properties and methods + * + * @author weihubeats 2022-7-10 */ public abstract class PolarisProcessor implements BeanPostProcessor, PriorityOrdered { 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 f9f255691..90e83f080 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 @@ -29,13 +29,15 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.annotation.Bean; /** - *@author : wh - *@date : 2022/7/10 14:15 - *@description: + * Spring value processor of field or method which has @Value and xml config placeholders. + * + * SpringValueProcessor + * + * @author weihubeats 2022-7-10 */ public class SpringValueProcessor extends PolarisProcessor implements BeanFactoryPostProcessor, BeanFactoryAware { - private static final Logger logger = LoggerFactory.getLogger(SpringValueProcessor.class); + private static final Logger LOGGER = LoggerFactory.getLogger(SpringValueProcessor.class); private final PolarisConfigProperties polarisConfigProperties; private final PlaceholderHelper placeholderHelper; @@ -96,7 +98,7 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanFactor return; } if (method.getParameterTypes().length != 1) { - logger.error("Ignore @Value setter {}.{}, expecting 1 parameter, actual {} parameters", + LOGGER.error("Ignore @Value setter {}.{}, expecting 1 parameter, actual {} parameters", bean.getClass().getName(), method.getName(), method.getParameterTypes().length); return; } @@ -121,12 +123,12 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanFactor springValue = new SpringValue(key, value.value(), bean, beanName, method, false); } else { - logger.error("Apollo @Value annotation currently only support to be used on methods and fields, " + LOGGER.error("Polaris @Value annotation currently only support to be used on methods and fields, " + "but is used on {}", member.getClass()); return; } springValueRegistry.register(beanFactory, key, springValue); - logger.info("Monitoring {}", springValue); + LOGGER.info("Monitoring {}", springValue); } } @@ -148,10 +150,10 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanFactor SpringValue springValue = new SpringValue(definition.getKey(), definition.getPlaceholder(), bean, beanName, method, false); springValueRegistry.register(beanFactory, definition.getKey(), springValue); - logger.debug("Monitoring {}", springValue); + LOGGER.debug("Monitoring {}", springValue); } catch (Throwable ex) { - logger.error("Failed to enable auto update feature for {}.{}", bean.getClass(), + LOGGER.error("Failed to enable auto update feature for {}.{}", bean.getClass(), definition.getPropertyName()); } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/PlaceholderHelper.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/PlaceholderHelper.java index f789bef49..9aae5a0c7 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/PlaceholderHelper.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/PlaceholderHelper.java @@ -30,9 +30,11 @@ import org.springframework.beans.factory.config.Scope; import org.springframework.util.StringUtils; /** - *@author : wh - *@date : 2022/7/10 14:26 - *@description: + * Placeholder helper functions. + * + * PlaceholderHelper + * + * @author weihubeats 2022-7-10 */ public class PlaceholderHelper { diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValue.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValue.java index ce67bd13a..501c58eef 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValue.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValue.java @@ -26,9 +26,11 @@ import java.lang.reflect.Type; import org.springframework.core.MethodParameter; /** - *@author : wh - *@date : 2022/7/10 14:23 - *@description: + * Spring @Value method info. + * + * SpringValue + * + * @author weihubeats 2022-7-10 */ public class SpringValue { diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinition.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinition.java index f5fa55e1f..4f73edb77 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinition.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinition.java @@ -18,10 +18,13 @@ package com.tencent.cloud.polaris.config.spring.property; /** - *@author : wh - *@date : 2022/7/10 14:24 - *@description: + * Spring value. + * + * SpringValueDefinition + * + * @author weihubeats 2022-7-10 */ + public class SpringValueDefinition { private final String key; diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessor.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessor.java index e750710b6..5dfa95c1c 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessor.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessor.java @@ -37,9 +37,19 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; /** - *@author : wh - *@date : 2022/7/10 14:25 - *@description: + * To process xml config placeholders, e.g. + * + *
+ *  <bean class="com.ctrip.framework.apollo.demo.spring.xmlConfigDemo.bean.XmlBean">
+ *    <property name="timeout" value="${timeout:200}"/>
+ *    <property name="batch" value="${batch:100}"/>
+ *  </bean>
+ * 
+ * + * + * SpringValueDefinitionProcessor + * + * @author weihubeats 2022-7-10 */ public class SpringValueDefinitionProcessor implements BeanDefinitionRegistryPostProcessor { private static final Map> beanName2SpringValueDefinitions = 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 e27d88f26..6bf09c770 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 @@ -35,9 +35,11 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.BeanFactory; /** - *@author : wh - *@date : 2022/7/10 14:29 - *@description: + * Spring value auto registry. + * + * SpringValueRegistry + * + * @author weihubeats 2022-7-10 */ public class SpringValueRegistry { private static final Logger logger = LoggerFactory.getLogger(SpringValueRegistry.class); diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/MockedConfigChange.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/MockedConfigChange.java index 1de1b4bf7..c1a20c9c0 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/MockedConfigChange.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/MockedConfigChange.java @@ -1,9 +1,9 @@ package com.tencent.cloud.polaris.config.adapter; /** - *@author : wh - *@date : 2022/7/10 14:50 - *@description: + * Mock config kv file for test. + * + * @author weihubeats 2022-7-10 */ public class MockedConfigChange { 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 b25a6cfb4..36360257a 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 @@ -123,6 +123,5 @@ public class PolarisPropertiesSourceAutoRefresherTest { Assert.assertEquals("v3", polarisPropertySource.getProperty("k3")); Assert.assertNull(polarisPropertySource.getProperty("k2")); Assert.assertEquals("v4", polarisPropertySource.getProperty("k4")); - verify(contextRefresher).refresh(); } }