@Reference(check = false/*,url = "dubbo://localhost:20880"*/,group = "helloA"/*,version = "*"*/)
private IHelloService helloServiceA;
使用这个注解后,在spring的容器中找不到这个Bean,那他是怎么注入的呢,
dubbo中AnnotationBean
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
if (!isMatchPackage(bean)) {//判断是否dubbo指定扫描的包
return bean;
}
Class<?> clazz = bean.getClass();
if (AopUtils.isAopProxy(bean)) {
clazz = AopUtils.getTargetClass(bean);
}
//如果获取值还是代理,则需要进一步解析
if (clazz.getSimpleName().contains("$$")) {
clazz = clazz.getSuperclass();
}
Method[] methods = clazz.getMethods();
for (Method method : methods) {
String name = method.getName();
if (name.length() > 3 && name.startsWith("set")
&& method.getParameterTypes().length == 1
&& Modifier.isPublic(method.getModifiers())
&& !Modifier.isStatic(method.getModifiers())) {
try {
Reference reference = method.getAnnotation(Reference.class);
if (reference != null) {
Object value = refer(reference, method.getParameterTypes()[0]);
if (value != null) {
method.invoke(bean, new Object[]{});
}
}
} catch (Throwable e) {
logger.error("Failed to init remote service reference at method " + name + " in class " + bean.getClass().getName() + ", cause: " + e.getMessage(), e);
}
}
}
Field[] fields = bean.getClass().getDeclaredFields();
for (Field field : fields) {
try {
if (!field.isAccessible()) {
field.setAccessible(true);
}
//判断使用指定了@Reference注解
Reference reference = field.getAnnotation(Reference.class);
if (reference != null) {
//创建代理对象
Object value = refer(reference, field.getType());
if (value != null) {
//给接口赋值,如例IHelloService helloServiceA
field.set(bean, value);
}
}
} catch (Throwable e) {
logger.error("Failed to init remote service reference at filed " + field.getName() + " in class " + bean.getClass().getName() + ", cause: " + e.getMessage(), e);
}
}
return bean;
}