diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java index 1063b23df..3e9d8cc6a 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java @@ -272,19 +272,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { } public static PolarisPropertySource loadPolarisPropertySource(ConfigFileService configFileService, String namespace, String group, String fileName) { - ConfigKVFile configKVFile; - // 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); - - throw new IllegalStateException("Only configuration files in the format of properties / yaml / yaml" + " can be injected into the spring context"); - } + ConfigKVFile configKVFile = loadConfigKVFile(configFileService, namespace, group, fileName); Map map = new ConcurrentHashMap<>(); for (String key : configKVFile.getPropertyNames()) { @@ -304,19 +292,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { for (ConfigFileMetadata configFile : remoteGroup.getConfigFileMetadataList()) { String fileName = configFile.getFileName(); - 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)) { - configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName); - } - else { - LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace, group, fileName); - - throw new IllegalStateException("Only configuration files in the format of properties / yaml / yaml" + " can be injected into the spring context"); - } + ConfigKVFile configKVFile = loadConfigKVFile(configFileService, namespace, group, fileName); configKVFiles.add(configKVFile); } @@ -336,4 +312,21 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { return new PolarisPropertySource(namespace, group, "", compositeConfigFile, map); } + + 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)) { + configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName); + } + else { + LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace, group, fileName); + + throw new IllegalStateException("Only configuration files in the format of properties / yaml / yaml" + " can be injected into the spring context"); + } + return configKVFile; + } } 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 b678129f9..0656a1069 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 @@ -130,7 +130,7 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanDefini } if (method.getAnnotation(RefreshScope.class) != null) { - processMethodRefreshScope(bean, beanName, method); + processMethodRefreshScope(bean, method); } } @@ -147,7 +147,12 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanDefini doRegister(bean, beanName, method, value, isRefreshScope); } - private void processMethodRefreshScope(Object bean, String beanName, Method method) { + /** + * @RefreshScope on method. + * @param bean spring bean. + * @param method method. + */ + private void processMethodRefreshScope(Object bean, Method method) { // must have @Bean annotation if (method.getAnnotation(Bean.class) == null) { return; @@ -156,27 +161,38 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanDefini for (Parameter parameter : method.getParameters()) { Value value = parameter.getAnnotation(Value.class); if (value != null) { + // method parameter with @Value Set keys = placeholderHelper.extractPlaceholderKeys(value.value()); springValueRegistry.putRefreshScopeKeys(keys); } + // method parameter class with @ConfigurationProperties ConfigurationProperties configurationProperties = parameter.getType().getAnnotation(ConfigurationProperties.class); parseConfigurationPropertiesKeys(configurationProperties, parameter.getType()); } + // analyze all fields of the class containing the method. for (Field field : findAllField(bean.getClass())) { Value value = field.getAnnotation(Value.class); if (value != null) { + // field with @Value Set keys = placeholderHelper.extractPlaceholderKeys(value.value()); springValueRegistry.putRefreshScopeKeys(keys); continue; } + // field class with @ConfigurationProperties ConfigurationProperties configurationProperties = field.getType().getAnnotation(ConfigurationProperties.class); parseConfigurationPropertiesKeys(configurationProperties, field.getType()); } } + /** + * parse refresh scope keys from @ConfigurationProperties. + * @param configurationProperties @ConfigurationProperties annotation object. + * @param clazz class of @ConfigurationProperties bean. + */ private void parseConfigurationPropertiesKeys(ConfigurationProperties configurationProperties, Class clazz) { if (configurationProperties != null) { + // get prefix from @ConfigurationProperties prefix or value. String prefix = configurationProperties.value(); if (StringUtils.isEmpty(prefix)) { prefix = configurationProperties.prefix(); @@ -188,12 +204,17 @@ 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. + */ private void parseConfigKeys(Class configClazz, String prefix) { for (Field field : findAllField(configClazz)) { if (isPrimitiveOrWrapper(field.getType())) { - // lowerCamel + // lowerCamel format springValueRegistry.putRefreshScopeKey(prefix + field.getName()); - // lower-hyphen + // lower-hyphen format springValueRegistry.putRefreshScopeKey( prefix + CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, field.getName())); } @@ -203,6 +224,7 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanDefini prefix + CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, field.getName())); } else { + // complex type, recursive parse parseConfigKeys(field.getType(), prefix + field.getName() + "."); parseConfigKeys(field.getType(), prefix + CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, field.getName()) + "."); @@ -210,6 +232,11 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanDefini } } + /** + * whether the class is primitive or wrapper. + * @param clazz the class under analysis. + * @return true if the class is primitive or wrapper, otherwise false. + */ private static boolean isPrimitiveOrWrapper(Class clazz) { return clazz.isPrimitive() || clazz == String.class || @@ -223,6 +250,11 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanDefini clazz == Double.class; } + /** + * whether the class is collection(array, collection, map). + * @param clazz the class under analysis. + * @return true if the class is collection(array, collection, map), otherwise false. + */ private static boolean isCollection(Class clazz) { return clazz.isArray() || Collection.class.isAssignableFrom(clazz) || Map.class.isAssignableFrom(clazz); }