如配置:@Reference(group = "${group_normal}")
public UserService userService;
group属性不生效
需要改动DubboConsumerAutoConfiguration的getConsumerBean方法中对reference进行转换,调用方法parseReferenceProperty(reference, environment)。
private <T> ReferenceBean<T> getConsumerBean(String beanName, Field field, Reference reference)
throws BeansException {
Environment environment = this.applicationContext.getEnvironment();
reference = parseReferenceProperty(reference, environment);
ReferenceBean<T> referenceBean = new ReferenceBean<T>(reference);
if ((reference.interfaceClass() == null || reference.interfaceClass() == void.class)
&& (reference.interfaceName() == null || "".equals(reference.interfaceName()))) {
referenceBean.setInterface(field.getType());
}
String application = reference.application();
referenceBean.setApplication(this.parseApplication(application, this.properties, environment,
beanName, field.getName(), "application", application));
String module = reference.module();
referenceBean.setModule(this.parseModule(module, this.properties, environment, beanName,
field.getName(), "module", module));
String[] registries = reference.registry();
referenceBean.setRegistries(this.parseRegistries(registries, this.properties, environment,
beanName, field.getName(), "registry"));
String monitor = reference.monitor();
referenceBean.setMonitor(this.parseMonitor(monitor, this.properties, environment, beanName,
field.getName(), "monitor", monitor));
String consumer = reference.consumer();
referenceBean.setConsumer(this.parseConsumer(consumer, this.properties, environment, beanName,
field.getName(), "consumer", consumer));
referenceBean.setApplicationContext(DubboConsumerAutoConfiguration.this.applicationContext);
return referenceBean;
}
protected Reference parseReferenceProperty(Reference reference, Environment environment) {
return parseAnnotation(Reference.class, reference, environment);
}
protected Reference parseAnnotation(Class<?> annotationClass, Reference annotation, Environment environment) {
Method[] methods = annotationClass.getMethods();
for (Method method : methods) {
if (method.getDeclaringClass() != Object.class
&& method.getReturnType() != void.class
&& method.getParameterTypes().length == 0
&& Modifier.isPublic(method.getModifiers())
&& !Modifier.isStatic(method.getModifiers())) {
try {
String property = method.getName();
Object value = method.invoke(annotation, new Object[0]);
//Class<?> parameterType = ReflectUtils.getBoxedClass(method.getReturnType());
if (value instanceof String) {// only convert string type
value = environment.resolvePlaceholders(value.toString());
if (value != null) {
InvocationHandler h = Proxy.getInvocationHandler(annotation);
// 获取 AnnotationInvocationHandler 的 memberValues 字段
Field hField = h.getClass().getDeclaredField("memberValues");
// 因为这个字段事 private final 修饰,所以要打开权限
hField.setAccessible(true);
// 获取 memberValues
Map memberValues = (Map) hField.get(h);
// 修改 value 属性值
memberValues.put(property, value);
}
}
} catch (Throwable e) {
}
}
}
return annotation;
}
对reference进行属性转换后就可以使用占位符属性配置项了
@Reference(group = "${group_normal}")
其中关键代码是Environment environment的environment.resolvePlaceholders(value.toString())方法