Skip to content

Commit

Permalink
Merge pull request nacos-group#58 from hxy1991/master
Browse files Browse the repository at this point in the history
  • Loading branch information
hxy1991 authored Sep 19, 2018
2 parents f9f9584 + ae6b0e7 commit 106dd16
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.InjectionMetadata;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.util.ReflectionUtils;
import org.springframework.validation.DataBinder;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import static com.alibaba.nacos.spring.util.NacosUtils.toProperties;
import static org.springframework.core.annotation.AnnotationUtils.getAnnotation;
Expand Down Expand Up @@ -95,32 +100,59 @@ public Object postProcessBeforeInitialization(Object bean, final String beanName

final Map<String, List<BeanProperty>> beanNamePropertyListMap = new HashMap<String, List<BeanProperty>>();

doWithAnnotationFields(bean, beanName, beanNamePropertyListMap);

List<BeanProperty> beanPropertyList = beanNamePropertyListMap.get(beanName);
if (beanPropertyList != null) {
for (BeanProperty beanProperty : beanPropertyList) {
put2ListMap(propertyBeanListMap, beanProperty, bean);
}
}

return super.postProcessBeforeInitialization(bean, beanName);
}

private void doWithAnnotationFields(Object bean, final String beanName,
final Map<String, List<BeanProperty>> beanNamePropertyListMap) {
ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() {
@Override
public void doWith(Field field) throws IllegalArgumentException {

doWithNacosValueField(field);

doWithValueField(field);
}

private void doWithNacosValueField(Field field) {
NacosValue annotation = getAnnotation(field, NacosValue.class);
if (annotation != null) {
if (Modifier.isStatic(field.getModifiers())) {
return;
}
if (annotation.autoRefreshed()) {
String placeHolder = annotation.value();
BeanProperty beanProperty = new BeanProperty(field.getName(), placeHolder);
put2ListMap(beanNamePropertyListMap, beanName, beanProperty);
put2ListMap(placeholderPropertyListMap, placeHolder, beanProperty);
doWithListMap(field, placeHolder);
}
}
}
});

List<BeanProperty> beanPropertyList = beanNamePropertyListMap.get(beanName);
if (beanPropertyList != null) {
for (BeanProperty beanProperty : beanPropertyList) {
put2ListMap(propertyBeanListMap, beanProperty, bean);
private void doWithValueField(Field field) {
Value annotation = getAnnotation(field, Value.class);
if (annotation != null) {
if (Modifier.isStatic(field.getModifiers())) {
return;
}
String placeHolder = annotation.value();
doWithListMap(field, placeHolder);
}
}
}

return super.postProcessBeforeInitialization(bean, beanName);
private void doWithListMap(Field field, String placeHolder) {
BeanProperty beanProperty = new BeanProperty(field.getName(), placeHolder);
put2ListMap(beanNamePropertyListMap, beanName, beanProperty);
put2ListMap(placeholderPropertyListMap, placeHolder, beanProperty);
}
});
}

private <K, V> void put2ListMap(Map<K, List<V>> map, K key, V value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
/**
* {@link NacosPropertySource} {@link Value} Test
*
* @author <a href="mailto:[email protected]">hxy1018</a>
* @author <a href="mailto:[email protected]">hxy1991</a>
* @see NacosPropertySource
* @see Value
* @since 0.1.0
Expand Down Expand Up @@ -134,7 +134,7 @@ public void testValue() throws NacosException, InterruptedException {

Thread.sleep(1000);

Assert.assertEquals(APP_NAME, app.name);
Assert.assertEquals(ANOTHER_APP_NAME, app.name);

Assert.assertEquals(ANOTHER_APP_NAME, app.nacosNameAutoRefreshed);

Expand Down

0 comments on commit 106dd16

Please sign in to comment.