Skip to content

Commit

Permalink
support nacos annotationadd defaultValue,fix float type bug
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyiyue1102 committed Sep 12, 2024
1 parent a082f9f commit 7eeafe0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class NacosAnnotationProcessor implements BeanPostProcessor, PriorityOrde

private final static Logger log = LoggerFactory
.getLogger(NacosAnnotationProcessor.class);

@Override
public int getOrder() {
return 0;
Expand Down Expand Up @@ -153,7 +154,7 @@ public void configChanged(ConfigChangeEvent event) {

@Override
public String toString() {
return String.format("sca nacos config listener on bean method %s", beanName + "#" + method.getName());
return String.format("sca nacos config listener on bean method %s", beanName + "@" + bean.hashCode() + "#" + method.getName());
}
});
}
Expand Down Expand Up @@ -197,7 +198,7 @@ public void configChanged(ConfigChangeEvent event) {

@Override
public String toString() {
return String.format("sca nacos config listener on bean method %s", beanName + "#" + method.getName());
return String.format("sca nacos config listener on bean method %s", beanName + "@" + bean.hashCode() + "#" + method.getName());
}
});

Expand Down Expand Up @@ -228,7 +229,7 @@ public void receiveConfigInfo(String configInfo) {

@Override
public String toString() {
return String.format("sca nacos config listener on bean method %s", beanName + "#" + method.getName());
return String.format("sca nacos config listener on bean method %s", beanName + "@" + bean.hashCode() + "#" + method.getName());
}
});

Expand All @@ -246,10 +247,10 @@ private void handleFiledNacosConfigAnnotation(NacosConfig annotation, String bea
ReflectionUtils.makeAccessible(field);

if (StringUtils.isBlank(key)) {
handleFiledNacosConfigAnnotationWithoutKey(dataId, group, beanName, bean, field);
handleFiledNacosConfigAnnotationWithoutKey(dataId, group, beanName, bean, field, annotation.defaultValue());
}
else {
handleFiledNacosConfigAnnotationWithKey(dataId, group, key, beanName, bean, field);
handleFiledNacosConfigAnnotationWithKey(dataId, group, key, beanName, bean, field, annotation.defaultValue());
}

}
Expand All @@ -258,13 +259,15 @@ private void handleFiledNacosConfigAnnotation(NacosConfig annotation, String bea
}
}

private void handleFiledNacosConfigAnnotationWithoutKey(String dataId, String group, String beanName, Object bean, Field field) {
private void handleFiledNacosConfigAnnotationWithoutKey(String dataId, String group, String beanName, Object bean, Field field, String defaultValue) {

try {
ReflectionUtils.makeAccessible(field);

String config = getGroupKeyContent(dataId, group);

if (config == null) {
config = defaultValue;
}
// annotation on string.
if (String.class.isAssignableFrom(field.getType())) {
ReflectionUtils.setField(field, bean, config);
Expand All @@ -281,7 +284,7 @@ public void receiveConfigInfo(String configInfo) {

@Override
public String toString() {
return String.format("sca nacos config listener on bean filed %s", beanName + "#" + field.getName());
return String.format("sca nacos config listener on bean filed %s", beanName + "@" + bean.hashCode() + "#" + field.getName());
}
});
return;
Expand Down Expand Up @@ -317,7 +320,7 @@ public void receiveConfigInfo(String configInfo) {

@Override
public String toString() {
return String.format("sca nacos config properties listener on bean filed %s", beanName + "#" + field.getName());
return String.format("sca nacos config properties listener on bean filed %s", beanName + "@" + bean.hashCode() + "#" + field.getName());
}

});
Expand Down Expand Up @@ -352,7 +355,7 @@ public void receiveConfigInfo(String configInfo) {

@Override
public String toString() {
return String.format("sca nacos config object listener on bean filed %s", beanName + "#" + field.getName());
return String.format("sca nacos config object listener on bean filed %s", beanName + "@" + bean.hashCode() + "#" + field.getName());
}

});
Expand All @@ -364,12 +367,14 @@ public String toString() {
}

private void handleFiledNacosConfigAnnotationWithKey(String dataId, String group, String key, String beanName, Object bean,
Field field) {
Field field, String defaultValue) {
try {
ReflectionUtils.makeAccessible(field);

String config = getDestContent(getGroupKeyContent(dataId, group), key);

if (config == null) {
config = defaultValue;
}
// annotation on string.
if (String.class.isAssignableFrom(field.getType())) {
ReflectionUtils.setField(field, bean, config);
Expand All @@ -385,7 +390,7 @@ public void configChanged(ConfigChangeEvent event) {

@Override
public String toString() {
return String.format("[spring cloud alibaba nacos config key listener , key %s , target %s ] ", key, beanName + "#" + field.getName());
return String.format("[spring cloud alibaba nacos config key listener , key %s , target %s ] ", key, beanName + "@" + bean.hashCode() + "#" + field.getName());
}

});
Expand Down Expand Up @@ -428,7 +433,7 @@ public void configChanged(ConfigChangeEvent event) {

@Override
public String toString() {
return String.format("[spring cloud alibaba nacos config key listener for properties , key %s , target %s ] ", key, beanName + "#" + field.getName());
return String.format("[spring cloud alibaba nacos config key listener for properties , key %s , target %s ] ", key, beanName + "@" + bean.hashCode() + "#" + field.getName());
}

});
Expand Down Expand Up @@ -456,7 +461,7 @@ public void configChanged(ConfigChangeEvent event) {

@Override
public String toString() {
return String.format("[spring cloud alibaba nacos config key listener , key %s , target %s ] ", key, beanName + "#" + field.getName());
return String.format("[spring cloud alibaba nacos config key listener , key %s , target %s ] ", key, beanName + "@" + bean.hashCode() + "#" + field.getName());
}
});
return;
Expand Down Expand Up @@ -526,7 +531,7 @@ else if (filed.getType() == Double.class) {
ReflectionUtils.setField(filed, bean, Double.valueOf(value));
}
else if (filed.getType() == float.class) {
filed.setDouble(bean, Float.valueOf(value));
filed.setFloat(bean, Float.valueOf(value));
}
else if (filed.getType() == Float.class) {
ReflectionUtils.setField(filed, bean, Float.valueOf(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@
String dataId();

String key() default "";

String defaultValue() default "";
}

0 comments on commit 7eeafe0

Please sign in to comment.