optimize:optimize config module code

pull/520/head
wulingxiao 3 years ago
parent e7d5e2a67d
commit f968892eae

@ -27,6 +27,7 @@ import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered; import org.springframework.core.PriorityOrdered;
import org.springframework.lang.NonNull;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
/** /**
@ -37,9 +38,9 @@ import org.springframework.util.ReflectionUtils;
public abstract class PolarisProcessor implements BeanPostProcessor, PriorityOrdered { public abstract class PolarisProcessor implements BeanPostProcessor, PriorityOrdered {
@Override @Override
public Object postProcessBeforeInitialization(Object bean, String beanName) public Object postProcessBeforeInitialization(Object bean, @NonNull String beanName)
throws BeansException { throws BeansException {
Class clazz = bean.getClass(); Class<?> clazz = bean.getClass();
for (Field field : findAllField(clazz)) { for (Field field : findAllField(clazz)) {
processField(bean, beanName, field); processField(bean, beanName, field);
} }
@ -50,7 +51,7 @@ public abstract class PolarisProcessor implements BeanPostProcessor, PriorityOrd
} }
@Override @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException {
return bean; return bean;
} }
@ -77,15 +78,15 @@ public abstract class PolarisProcessor implements BeanPostProcessor, PriorityOrd
return Ordered.LOWEST_PRECEDENCE; return Ordered.LOWEST_PRECEDENCE;
} }
private List<Field> findAllField(Class clazz) { private List<Field> findAllField(Class<?> clazz) {
final List<Field> res = new LinkedList<>(); final List<Field> res = new LinkedList<>();
ReflectionUtils.doWithFields(clazz, field -> res.add(field)); ReflectionUtils.doWithFields(clazz, res::add);
return res; return res;
} }
private List<Method> findAllMethod(Class clazz) { private List<Method> findAllMethod(Class<?> clazz) {
final List<Method> res = new LinkedList<>(); final List<Method> res = new LinkedList<>();
ReflectionUtils.doWithMethods(clazz, method -> res.add(method)); ReflectionUtils.doWithMethods(clazz, res::add);
return res; return res;
} }
} }

@ -87,7 +87,7 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanFactor
} }
@Override @Override
public Object postProcessBeforeInitialization(Object bean, String beanName) public Object postProcessBeforeInitialization(Object bean, @NonNull String beanName)
throws BeansException { throws BeansException {
if (polarisConfigProperties.isAutoRefresh()) { if (polarisConfigProperties.isAutoRefresh()) {
super.postProcessBeforeInitialization(bean, beanName); super.postProcessBeforeInitialization(bean, beanName);

@ -71,7 +71,7 @@ public class PlaceholderHelper {
if (beanFactory.getBeanExpressionResolver() == null) { if (beanFactory.getBeanExpressionResolver() == null) {
return value; return value;
} }
Scope scope = (beanDefinition != null ? beanFactory Scope scope = (beanDefinition != null && beanDefinition.getScope() != null ? beanFactory
.getRegisteredScope(beanDefinition.getScope()) : null); .getRegisteredScope(beanDefinition.getScope()) : null);
return beanFactory.getBeanExpressionResolver() return beanFactory.getBeanExpressionResolver()
.evaluate(value, new BeanExpressionContext(beanFactory, scope)); .evaluate(value, new BeanExpressionContext(beanFactory, scope));
@ -92,7 +92,7 @@ public class PlaceholderHelper {
public Set<String> extractPlaceholderKeys(String propertyString) { public Set<String> extractPlaceholderKeys(String propertyString) {
Set<String> placeholderKeys = Sets.newHashSet(); Set<String> placeholderKeys = Sets.newHashSet();
if (Strings.isNullOrEmpty(propertyString) || (!isNormalizedPlaceholder(propertyString) && !isExpressionWithPlaceholder(propertyString))) { if (!isPlaceholder(propertyString)) {
return placeholderKeys; return placeholderKeys;
} }
@ -147,6 +147,11 @@ public class PlaceholderHelper {
return placeholderKeys; return placeholderKeys;
} }
private boolean isPlaceholder(String propertyString) {
return !Strings.isNullOrEmpty(propertyString) &&
(isNormalizedPlaceholder(propertyString) || isExpressionWithPlaceholder(propertyString));
}
private boolean isNormalizedPlaceholder(String propertyString) { private boolean isNormalizedPlaceholder(String propertyString) {
return propertyString.startsWith(PLACEHOLDER_PREFIX) && propertyString.contains(PLACEHOLDER_SUFFIX); return propertyString.startsWith(PLACEHOLDER_PREFIX) && propertyString.contains(PLACEHOLDER_SUFFIX);
} }

@ -86,7 +86,7 @@ public class SpringValue {
private void injectMethod(Object newVal) private void injectMethod(Object newVal)
throws InvocationTargetException, IllegalAccessException { throws InvocationTargetException, IllegalAccessException {
Object bean = beanRef.get(); Object bean = beanRef.get();
if (bean == null) { if (bean == null || methodParameter.getMethod() == null) {
return; return;
} }
methodParameter.getMethod().invoke(bean, newVal); methodParameter.getMethod().invoke(bean, newVal);
@ -131,7 +131,12 @@ public class SpringValue {
.format("key: %s, beanName: %s, field: %s.%s", key, beanName, bean.getClass() .format("key: %s, beanName: %s, field: %s.%s", key, beanName, bean.getClass()
.getName(), field.getName()); .getName(), field.getName());
} }
return String.format("key: %s, beanName: %s, method: %s.%s", key, beanName, bean.getClass().getName(), if (null != methodParameter.getMethod()) {
methodParameter.getMethod().getName()); return String.format("key: %s, beanName: %s, method: %s.%s", key, beanName, bean.getClass().getName(),
methodParameter.getMethod().getName());
}
else {
return String.format("key: %s, beanName: %s", key, beanName);
}
} }
} }

Loading…
Cancel
Save