Skip to content

Commit

Permalink
refactor: SpringValueProcessor extract duplicate code (#4137)
Browse files Browse the repository at this point in the history
* refactor: SpringValueProcessor extract duplicate code

* fix: typo & logging level
  • Loading branch information
mghio authored Dec 9, 2021
1 parent 765a24e commit 5e17275
Showing 1 changed file with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.common.collect.Multimap;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Set;
Expand All @@ -46,7 +47,7 @@
/**
* Spring value processor of field or method which has @Value and xml config placeholders.
*
* @author github.com/zhegexiaohuozi [email protected]
* @author github.com/zhegexiaohuozi [email protected] [email protected]
* @since 2017/12/20.
*/
public class SpringValueProcessor extends ApolloProcessor implements BeanFactoryPostProcessor, BeanFactoryAware {
Expand Down Expand Up @@ -94,17 +95,8 @@ protected void processField(Object bean, String beanName, Field field) {
if (value == null) {
return;
}
Set<String> keys = placeholderHelper.extractPlaceholderKeys(value.value());

if (keys.isEmpty()) {
return;
}

for (String key : keys) {
SpringValue springValue = new SpringValue(key, value.value(), bean, beanName, field, false);
springValueRegistry.register(beanFactory, key, springValue);
logger.debug("Monitoring {}", springValue);
}
doRegister(bean, beanName, field, value);
}

@Override
Expand All @@ -124,20 +116,33 @@ protected void processMethod(Object bean, String beanName, Method method) {
return;
}

Set<String> keys = placeholderHelper.extractPlaceholderKeys(value.value());
doRegister(bean, beanName, method, value);
}

private void doRegister(Object bean, String beanName, Member member, Value value) {
Set<String> keys = placeholderHelper.extractPlaceholderKeys(value.value());
if (keys.isEmpty()) {
return;
}

for (String key : keys) {
SpringValue springValue = new SpringValue(key, value.value(), bean, beanName, method, false);
SpringValue springValue;
if (member instanceof Field) {
Field field = (Field) member;
springValue = new SpringValue(key, value.value(), bean, beanName, field, false);
} else if (member instanceof Method) {
Method method = (Method) member;
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, "
+ "but is used on {}", member.getClass());
return;
}
springValueRegistry.register(beanFactory, key, springValue);
logger.info("Monitoring {}", springValue);
}
}


private void processBeanPropertyValues(Object bean, String beanName) {
Collection<SpringValueDefinition> propertySpringValues = beanName2SpringValueDefinitions
.get(beanName);
Expand Down

0 comments on commit 5e17275

Please sign in to comment.