pull/1463/head
shedfreewu 10 months ago
parent 1fc1680f45
commit 51a86fd140

@ -272,19 +272,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
} }
public static PolarisPropertySource loadPolarisPropertySource(ConfigFileService configFileService, String namespace, String group, String fileName) { public static PolarisPropertySource loadPolarisPropertySource(ConfigFileService configFileService, String namespace, String group, String fileName) {
ConfigKVFile configKVFile; ConfigKVFile configKVFile = loadConfigKVFile(configFileService, namespace, group, 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);
throw new IllegalStateException("Only configuration files in the format of properties / yaml / yaml" + " can be injected into the spring context");
}
Map<String, Object> map = new ConcurrentHashMap<>(); Map<String, Object> map = new ConcurrentHashMap<>();
for (String key : configKVFile.getPropertyNames()) { for (String key : configKVFile.getPropertyNames()) {
@ -304,19 +292,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
for (ConfigFileMetadata configFile : remoteGroup.getConfigFileMetadataList()) { for (ConfigFileMetadata configFile : remoteGroup.getConfigFileMetadataList()) {
String fileName = configFile.getFileName(); String fileName = configFile.getFileName();
ConfigKVFile configKVFile; ConfigKVFile configKVFile = loadConfigKVFile(configFileService, namespace, group, fileName);
// 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");
}
configKVFiles.add(configKVFile); configKVFiles.add(configKVFile);
} }
@ -336,4 +312,21 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
return new PolarisPropertySource(namespace, group, "", compositeConfigFile, map); 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;
}
} }

@ -130,7 +130,7 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanDefini
} }
if (method.getAnnotation(RefreshScope.class) != null) { 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); 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 // must have @Bean annotation
if (method.getAnnotation(Bean.class) == null) { if (method.getAnnotation(Bean.class) == null) {
return; return;
@ -156,27 +161,38 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanDefini
for (Parameter parameter : method.getParameters()) { for (Parameter parameter : method.getParameters()) {
Value value = parameter.getAnnotation(Value.class); Value value = parameter.getAnnotation(Value.class);
if (value != null) { if (value != null) {
// method parameter with @Value
Set<String> keys = placeholderHelper.extractPlaceholderKeys(value.value()); Set<String> keys = placeholderHelper.extractPlaceholderKeys(value.value());
springValueRegistry.putRefreshScopeKeys(keys); springValueRegistry.putRefreshScopeKeys(keys);
} }
// method parameter class with @ConfigurationProperties
ConfigurationProperties configurationProperties = parameter.getType().getAnnotation(ConfigurationProperties.class); ConfigurationProperties configurationProperties = parameter.getType().getAnnotation(ConfigurationProperties.class);
parseConfigurationPropertiesKeys(configurationProperties, parameter.getType()); parseConfigurationPropertiesKeys(configurationProperties, parameter.getType());
} }
// analyze all fields of the class containing the method.
for (Field field : findAllField(bean.getClass())) { for (Field field : findAllField(bean.getClass())) {
Value value = field.getAnnotation(Value.class); Value value = field.getAnnotation(Value.class);
if (value != null) { if (value != null) {
// field with @Value
Set<String> keys = placeholderHelper.extractPlaceholderKeys(value.value()); Set<String> keys = placeholderHelper.extractPlaceholderKeys(value.value());
springValueRegistry.putRefreshScopeKeys(keys); springValueRegistry.putRefreshScopeKeys(keys);
continue; continue;
} }
// field class with @ConfigurationProperties
ConfigurationProperties configurationProperties = field.getType().getAnnotation(ConfigurationProperties.class); ConfigurationProperties configurationProperties = field.getType().getAnnotation(ConfigurationProperties.class);
parseConfigurationPropertiesKeys(configurationProperties, field.getType()); 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) { private void parseConfigurationPropertiesKeys(ConfigurationProperties configurationProperties, Class<?> clazz) {
if (configurationProperties != null) { if (configurationProperties != null) {
// get prefix from @ConfigurationProperties prefix or value.
String prefix = configurationProperties.value(); String prefix = configurationProperties.value();
if (StringUtils.isEmpty(prefix)) { if (StringUtils.isEmpty(prefix)) {
prefix = configurationProperties.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) { private void parseConfigKeys(Class<?> configClazz, String prefix) {
for (Field field : findAllField(configClazz)) { for (Field field : findAllField(configClazz)) {
if (isPrimitiveOrWrapper(field.getType())) { if (isPrimitiveOrWrapper(field.getType())) {
// lowerCamel // lowerCamel format
springValueRegistry.putRefreshScopeKey(prefix + field.getName()); springValueRegistry.putRefreshScopeKey(prefix + field.getName());
// lower-hyphen // lower-hyphen format
springValueRegistry.putRefreshScopeKey( springValueRegistry.putRefreshScopeKey(
prefix + CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, field.getName())); 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())); prefix + CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, field.getName()));
} }
else { else {
// complex type, recursive parse
parseConfigKeys(field.getType(), prefix + field.getName() + "."); parseConfigKeys(field.getType(), prefix + field.getName() + ".");
parseConfigKeys(field.getType(), parseConfigKeys(field.getType(),
prefix + CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, field.getName()) + "."); 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) { private static boolean isPrimitiveOrWrapper(Class<?> clazz) {
return clazz.isPrimitive() || return clazz.isPrimitive() ||
clazz == String.class || clazz == String.class ||
@ -223,6 +250,11 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanDefini
clazz == Double.class; 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) { private static boolean isCollection(Class<?> clazz) {
return clazz.isArray() || Collection.class.isAssignableFrom(clazz) || Map.class.isAssignableFrom(clazz); return clazz.isArray() || Collection.class.isAssignableFrom(clazz) || Map.class.isAssignableFrom(clazz);
} }

Loading…
Cancel
Save