Dubbo之——改造Dubbo,使其能够兼容Spring 4注解配置

本文涉及的产品
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: Dubbo之——改造Dubbo,使其能够兼容Spring 4注解配置

Dubbo之——改造Dubbo,使其能够兼容Spring 4注解配置

转载请注明出处:http://blog.csdn.net/l1028386804/article/details/70040928

而随着Spring Boot的大热,Java-Base方式配置Spring也变得越来越流行。
Dubbo + Boot的开发模式,也是较为常见的组合方式。
但是,当使用Dubbo在高版本Spring环境中使用注解方式配置时,会因为一些代码版本的原因导致整合出现问题。

  1. Dubbo原生的注解配置
    Dubbo本身就是基于Spring的,而且原生就提供注解配置:

服务提供方配置:

服务提供方注解:
[java] view plain copy
import com.alibaba.dubbo.config.annotation.Service;

@Service(version=”1.0.0”)
public class FooServiceImpl implements FooService {
// ……
}
服务消费方注解:
[java] view plain copy
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

@Component
public class BarAction {

@Reference(version="1.0.0")  
private FooService fooService;

}
服务消费方配置:
[html] view plain copy

通过官方的例子,就可以看出Dubbo使用xml配置 来开启注解配置,并提供 com.alibaba.dubbo.config.annotation.Service注解进行服务注册,提供com.alibaba.dubbo.config.annotation.Reference注解进行服务注入。
2.实现机制
可以看出,内部机制都是依托于标签。 通过源码分析,Dubbo对于Spring xml解析处理由com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler提供:
DubboNamespaceHandler.java
[java] view plain copy
package com.alibaba.dubbo.config.spring.schema;

import org.springframework.beans.factory.xml.NamespaceHandlerSupport;

import com.alibaba.dubbo.common.Version;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.ModuleConfig;
import com.alibaba.dubbo.config.MonitorConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.AnnotationBean;
import com.alibaba.dubbo.config.spring.ReferenceBean;
import com.alibaba.dubbo.config.spring.ServiceBean;

/**

  • DubboNamespaceHandler
  • @author william.liangf
  • @export
    */

public class DubboNamespaceHandler extends NamespaceHandlerSupport {

static {  
    Version.checkDuplicate(DubboNamespaceHandler.class);  
}  

public void init() {  
    registerBeanDefinitionParser("application", new DubboBeanDefinitionParser(ApplicationConfig.class, true));  
    registerBeanDefinitionParser("module", new DubboBeanDefinitionParser(ModuleConfig.class, true));  
    registerBeanDefinitionParser("registry", new DubboBeanDefinitionParser(RegistryConfig.class, true));  
    registerBeanDefinitionParser("monitor", new DubboBeanDefinitionParser(MonitorConfig.class, true));  
    registerBeanDefinitionParser("provider", new DubboBeanDefinitionParser(ProviderConfig.class, true));  
    registerBeanDefinitionParser("consumer", new DubboBeanDefinitionParser(ConsumerConfig.class, true));  
    registerBeanDefinitionParser("protocol", new DubboBeanDefinitionParser(ProtocolConfig.class, true));  
    registerBeanDefinitionParser("service", new DubboBeanDefinitionParser(ServiceBean.class, true));  
    registerBeanDefinitionParser("reference", new DubboBeanDefinitionParser(ReferenceBean.class, false));  
    registerBeanDefinitionParser("annotation", new DubboBeanDefinitionParser(AnnotationBean.class, true));  
}

}
通过上面的代码可以很直观的发现,标签实际是由com.alibaba.dubbo.config.spring.schema.DubboBeanDefinitionParser解析:
DubboBeanDefinitionParser.java
[java] view plain copy
/**

  • AbstractBeanDefinitionParser
  • @author william.liangf
  • @export
    */

public class DubboBeanDefinitionParser implements BeanDefinitionParser {

private static final Logger logger = LoggerFactory.getLogger(DubboBeanDefinitionParser.class);  

private final Class<?> beanClass;  

private final boolean required;  

public DubboBeanDefinitionParser(Class<?> beanClass, boolean required) {  
    this.beanClass = beanClass;  
    this.required = required;  
}  

public BeanDefinition parse(Element element, ParserContext parserContext) {  
    return parse(element, parserContext, beanClass, required);  
}  

@SuppressWarnings("unchecked")  
private static BeanDefinition parse(Element element, ParserContext parserContext, Class<?> beanClass, boolean required) {  
    //略  
}

可以看到这个类实现了Spring的org.springframework.beans.factory.xml.BeanDefinitionParser接口,从而完成Spring Bean的解析工作。
而registerBeanDefinitionParser(“annotation”, new DubboBeanDefinitionParser(AnnotationBean.class, true));就是将标签,解析成com.alibaba.dubbo.config.spring.AnnotationBean并注册到Spring中。
3.AnnotationBean分析
先来看看源码:
AnnotationBean.java
[java] view plain copy
package com.alibaba.dubbo.config.spring;

/**

  • AnnotationBean
  • @author william.liangf
  • @export
    */

public class AnnotationBean extends AbstractConfig implements DisposableBean, BeanFactoryPostProcessor, BeanPostProcessor, ApplicationContextAware {

private static final long serialVersionUID = -7582802454287589552L;  

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

private String annotationPackage;  

private String[] annotationPackages;  

private final Set<ServiceConfig<?>> serviceConfigs = new ConcurrentHashSet<ServiceConfig<?>>();  

private final ConcurrentMap<String, ReferenceBean<?>> referenceConfigs = new ConcurrentHashMap<String, ReferenceBean<?>>();  

public String getPackage() {  
    return annotationPackage;  
}  

public void setPackage(String annotationPackage) {  
    this.annotationPackage = annotationPackage;  
    this.annotationPackages = (annotationPackage == null || annotationPackage.length() == 0) ? null  
            : Constants.COMMA_SPLIT_PATTERN.split(annotationPackage);  
}  

private ApplicationContext applicationContext;  

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {  
    this.applicationContext = applicationContext;  
}  

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)  
        throws BeansException {  
    if (annotationPackage == null || annotationPackage.length() == 0) {  
        return;  
    }  
    if (beanFactory instanceof BeanDefinitionRegistry) {  
        try {  
            // init scanner  
            Class<?> scannerClass = ReflectUtils.forName("org.springframework.context.annotation.ClassPathBeanDefinitionScanner");  
            Object scanner = scannerClass.getConstructor(new Class<?>[] {BeanDefinitionRegistry.class, boolean.class}).newInstance(new Object[] {(BeanDefinitionRegistry) beanFactory, true});  
            // add filter  
            Class<?> filterClass = ReflectUtils.forName("org.springframework.core.type.filter.AnnotationTypeFilter");  
            Object filter = filterClass.getConstructor(Class.class).newInstance(Service.class);  
            Method addIncludeFilter = scannerClass.getMethod("addIncludeFilter", ReflectUtils.forName("org.springframework.core.type.filter.TypeFilter"));  
            addIncludeFilter.invoke(scanner, filter);  
            // scan packages  
            String[] packages = Constants.COMMA_SPLIT_PATTERN.split(annotationPackage);  
            Method scan = scannerClass.getMethod("scan", new Class<?>[]{String[].class});  
            scan.invoke(scanner, new Object[] {packages});  
        } catch (Throwable e) {  
            // spring 2.0  
        }  
    }  
}  

public void destroy() throws Exception {  
    for (ServiceConfig<?> serviceConfig : serviceConfigs) {  
        try {  
            serviceConfig.unexport();  
        } catch (Throwable e) {  
            logger.error(e.getMessage(), e);  
        }  
    }  
    for (ReferenceConfig<?> referenceConfig : referenceConfigs.values()) {  
        try {  
            referenceConfig.destroy();  
        } catch (Throwable e) {  
            logger.error(e.getMessage(), e);  
        }  
    }  
}  

public Object postProcessAfterInitialization(Object bean, String beanName)  
        throws BeansException {  
    if (! isMatchPackage(bean)) {  
        return bean;  
    }  
    Service service = bean.getClass().getAnnotation(Service.class);  
    if (service != null) {  
        ServiceBean<Object> serviceConfig = new ServiceBean<Object>(service);  
        if (void.class.equals(service.interfaceClass())  
                && "".equals(service.interfaceName())) {  
            if (bean.getClass().getInterfaces().length > 0) {  
                serviceConfig.setInterface(bean.getClass().getInterfaces()[0]);  
            } else {  
                throw new IllegalStateException("Failed to export remote service class " + bean.getClass().getName() + ", cause: The @Service undefined interfaceClass or interfaceName, and the service class unimplemented any interfaces.");  
            }  
        }  
        if (applicationContext != null) {  
            serviceConfig.setApplicationContext(applicationContext);  
            if (service.registry() != null && service.registry().length > 0) {  
                List<RegistryConfig> registryConfigs = new ArrayList<RegistryConfig>();  
                for (String registryId : service.registry()) {  
                    if (registryId != null && registryId.length() > 0) {  
                        registryConfigs.add((RegistryConfig)applicationContext.getBean(registryId, RegistryConfig.class));  
                    }  
                }  
                serviceConfig.setRegistries(registryConfigs);  
            }  
            if (service.provider() != null && service.provider().length() > 0) {  
                serviceConfig.setProvider((ProviderConfig)applicationContext.getBean(service.provider(),ProviderConfig.class));  
            }  
            if (service.monitor() != null && service.monitor().length() > 0) {  
                serviceConfig.setMonitor((MonitorConfig)applicationContext.getBean(service.monitor(), MonitorConfig.class));  
            }  
            if (service.application() != null && service.application().length() > 0) {  
                serviceConfig.setApplication((ApplicationConfig)applicationContext.getBean(service.application(), ApplicationConfig.class));  
            }  
            if (service.module() != null && service.module().length() > 0) {  
                serviceConfig.setModule((ModuleConfig)applicationContext.getBean(service.module(), ModuleConfig.class));  
            }  
            if (service.provider() != null && service.provider().length() > 0) {  
                serviceConfig.setProvider((ProviderConfig)applicationContext.getBean(service.provider(), ProviderConfig.class));  
            } else {  

            }  
            if (service.protocol() != null && service.protocol().length > 0) {  
                List<ProtocolConfig> protocolConfigs = new ArrayList<ProtocolConfig>();  
                for (String protocolId : service.registry()) {  
                    if (protocolId != null && protocolId.length() > 0) {  
                        protocolConfigs.add((ProtocolConfig)applicationContext.getBean(protocolId, ProtocolConfig.class));  
                    }  
                }  
                serviceConfig.setProtocols(protocolConfigs);  
            }  
            try {  
                serviceConfig.afterPropertiesSet();  
            } catch (RuntimeException e) {  
                throw (RuntimeException) e;  
            } catch (Exception e) {  
                throw new IllegalStateException(e.getMessage(), e);  
            }  
        }  
        serviceConfig.setRef(bean);  
        serviceConfigs.add(serviceConfig);  
        serviceConfig.export();  
    }  
    return bean;  
}  

public Object postProcessBeforeInitialization(Object bean, String beanName)  
        throws BeansException {  
    if (! isMatchPackage(bean)) {  
        return bean;  
    }  
    Method[] methods = bean.getClass().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 = field.getAnnotation(Reference.class);  
            if (reference != null) {  
                Object value = refer(reference, field.getType());  
                if (value != null) {  
                    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;  
}  

private Object refer(Reference reference, Class<?> referenceClass) { //method.getParameterTypes()[0]  
    String interfaceName;  
    if (! "".equals(reference.interfaceName())) {  
        interfaceName = reference.interfaceName();  
    } else if (! void.class.equals(reference.interfaceClass())) {  
        interfaceName = reference.interfaceClass().getName();  
    } else if (referenceClass.isInterface()) {  
        interfaceName = referenceClass.getName();  
    } else {  
        throw new IllegalStateException("The @Reference undefined interfaceClass or interfaceName, and the property type " + referenceClass.getName() + " is not a interface.");  
    }  
    String key = reference.group() + "/" + interfaceName + ":" + reference.version();  
    ReferenceBean<?> referenceConfig = referenceConfigs.get(key);  
    if (referenceConfig == null) {  
        referenceConfig = new ReferenceBean<Object>(reference);  
        if (void.class.equals(reference.interfaceClass())  
                && "".equals(reference.interfaceName())  
                && referenceClass.isInterface()) {  
            referenceConfig.setInterface(referenceClass);  
        }  
        if (applicationContext != null) {  
            referenceConfig.setApplicationContext(applicationContext);  
            if (reference.registry() != null && reference.registry().length > 0) {  
                List<RegistryConfig> registryConfigs = new ArrayList<RegistryConfig>();  
                for (String registryId : reference.registry()) {  
                    if (registryId != null && registryId.length() > 0) {  
                        registryConfigs.add((RegistryConfig)applicationContext.getBean(registryId, RegistryConfig.class));  
                    }  
                }  
                referenceConfig.setRegistries(registryConfigs);  
            }  
            if (reference.consumer() != null && reference.consumer().length() > 0) {  
                referenceConfig.setConsumer((ConsumerConfig)applicationContext.getBean(reference.consumer(), ConsumerConfig.class));  
            }  
            if (reference.monitor() != null && reference.monitor().length() > 0) {  
                referenceConfig.setMonitor((MonitorConfig)applicationContext.getBean(reference.monitor(), MonitorConfig.class));  
            }  
            if (reference.application() != null && reference.application().length() > 0) {  
                referenceConfig.setApplication((ApplicationConfig)applicationContext.getBean(reference.application(), ApplicationConfig.class));  
            }  
            if (reference.module() != null && reference.module().length() > 0) {  
                referenceConfig.setModule((ModuleConfig)applicationContext.getBean(reference.module(), ModuleConfig.class));  
            }  
            if (reference.consumer() != null && reference.consumer().length() > 0) {  
                referenceConfig.setConsumer((ConsumerConfig)applicationContext.getBean(reference.consumer(), ConsumerConfig.class));  
            }  
            try {  
                referenceConfig.afterPropertiesSet();  
            } catch (RuntimeException e) {  
                throw (RuntimeException) e;  
            } catch (Exception e) {  
                throw new IllegalStateException(e.getMessage(), e);  
            }  
        }  
        referenceConfigs.putIfAbsent(key, referenceConfig);  
        referenceConfig = referenceConfigs.get(key);  
    }  
    return referenceConfig.get();  
}  

private boolean isMatchPackage(Object bean) {  
    if (annotationPackages == null || annotationPackages.length == 0) {  
        return true;  
    }  
    String beanClassName = bean.getClass().getName();  
    for (String pkg : annotationPackages) {  
        if (beanClassName.startsWith(pkg)) {  
            return true;  
        }  
    }  
    return false;  
}

}
这个AnnotationBean实现了几个Spring生命周期接口,从而完成Dubbo整合Spring 的操作。
org.springframework.beans.factory.config.BeanFactoryPostProcessor
先来看看Spring文档中的介绍:
[plain] view plain copy
BeanFactoryPostProcessor operates on the bean configuration metadata; that is, the Spring IoC container allows a BeanFactoryPostProcessor to read the configuration metadata and potentially change it before the container instantiates any beans other than BeanFactoryPostProcessors.
BeanFactoryPostProcessor可以用于在Spring IoC容器实例化Bean之前,对Spring Bean配置信息进行一些操作
通过Spring文档,可以清楚这个接口的功能,那再来看看Dubbo的AnnotationBean是如何实现这个接口的:
[java] view plain copy
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
if (annotationPackage == null || annotationPackage.length() == 0) {
return;
}
if (beanFactory instanceof BeanDefinitionRegistry) {
try {
// init scanner
Class

相关文章
|
16天前
|
Java Spring 容器
如何解决spring EL注解@Value获取值为null的问题
本文探讨了在使用Spring框架时,如何避免`@Value(&quot;${xxx.xxx}&quot;)`注解导致值为null的问题。通过具体示例分析了几种常见错误场景,包括类未交给Spring管理、字段被`static`或`final`修饰以及通过`new`而非依赖注入创建对象等,提出了相应的解决方案,并强调了理解框架原理的重要性。
56 4
|
6天前
|
Java API 数据库
构建RESTful API已经成为现代Web开发的标准做法之一。Spring Boot框架因其简洁的配置、快速的启动特性及丰富的功能集而备受开发者青睐。
【10月更文挑战第11天】本文介绍如何使用Spring Boot构建在线图书管理系统的RESTful API。通过创建Spring Boot项目,定义`Book`实体类、`BookRepository`接口和`BookService`服务类,最后实现`BookController`控制器来处理HTTP请求,展示了从基础环境搭建到API测试的完整过程。
21 4
|
3天前
|
Java API 数据库
Spring Boot框架因其简洁的配置、快速的启动特性及丰富的功能集而备受开发者青睐
本文通过在线图书管理系统案例,详细介绍如何使用Spring Boot构建RESTful API。从项目基础环境搭建、实体类与数据访问层定义,到业务逻辑实现和控制器编写,逐步展示了Spring Boot的简洁配置和强大功能。最后,通过Postman测试API,并介绍了如何添加安全性和异常处理,确保API的稳定性和安全性。
9 0
|
13天前
|
XML Java 数据格式
Spring从入门到入土(bean的一些子标签及注解的使用)
本文详细介绍了Spring框架中Bean的创建和使用,包括使用XML配置文件中的标签和注解来创建和管理Bean,以及如何通过构造器、Setter方法和属性注入来配置Bean。
53 9
Spring从入门到入土(bean的一些子标签及注解的使用)
|
2天前
|
Dubbo Java 应用服务中间件
Spring Cloud Dubbo:微服务通信的高效解决方案
【10月更文挑战第15天】随着信息技术的发展,微服务架构成为企业应用开发的主流。Spring Cloud Dubbo结合了Dubbo的高性能RPC和Spring Cloud的生态系统,提供高效、稳定的微服务通信解决方案。它支持多种通信协议,具备服务注册与发现、负载均衡及容错机制,简化了服务调用的复杂性,使开发者能更专注于业务逻辑的实现。
10 2
|
4天前
|
Java BI 调度
Java Spring的定时任务的配置和使用
遵循上述步骤,你就可以在Spring应用中轻松地配置和使用定时任务,满足各种定时处理需求。
24 1
|
5天前
|
架构师 Java 开发者
得物面试:Springboot自动装配机制是什么?如何控制一个bean 是否加载,使用什么注解?
在40岁老架构师尼恩的读者交流群中,近期多位读者成功获得了知名互联网企业的面试机会,如得物、阿里、滴滴等。然而,面对“Spring Boot自动装配机制”等核心面试题,部分读者因准备不足而未能顺利通过。为此,尼恩团队将系统化梳理和总结这一主题,帮助大家全面提升技术水平,让面试官“爱到不能自已”。
得物面试:Springboot自动装配机制是什么?如何控制一个bean 是否加载,使用什么注解?
|
10天前
|
XML Java 数据库
Spring boot的最全注解
Spring boot的最全注解
|
11天前
|
JSON NoSQL Java
springBoot:jwt&redis&文件操作&常见请求错误代码&参数注解 (九)
该文档涵盖JWT(JSON Web Token)的组成、依赖、工具类创建及拦截器配置,并介绍了Redis的依赖配置与文件操作相关功能,包括文件上传、下载、删除及批量删除的方法。同时,文档还列举了常见的HTTP请求错误代码及其含义,并详细解释了@RequestParam与@PathVariable等参数注解的区别与用法。
|
11天前
|
Java API Spring
springBoot:注解&封装类&异常类&登录实现类 (八)
本文介绍了Spring Boot项目中的一些关键代码片段,包括使用`@PathVariable`绑定路径参数、创建封装类Result和异常处理类GlobalException、定义常量接口Constants、自定义异常ServiceException以及实现用户登录功能。通过这些代码,展示了如何构建RESTful API,处理请求参数,统一返回结果格式,以及全局异常处理等核心功能。