Spring自定义schema
我们再使用dubbo进行时 会经常看到这样的标签
<!--开启注解扫描--> <context:component-scan base-package="com.liqiang.service.impl"></context:component-scan> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="userSystemProvider" /> <!-- 服务注册中心地址 其他参数请看用户指南--> <dubbo:registry address="zookeeper://192.168.65.128:2181?backup=192.168.65.128:2182,192.168.65.128:2183" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!--发布服务--> <dubbo:service interface="com.liqiang.service.UserService" ref="userServiceImpl"></dubbo:service> <!--发布服务--> <dubbo:service interface="com.liqiang.service.ProductService" ref="productServiceImpl"></dubbo:service>
其实是利用spring2.0的对自定义schema的支持实现的 。下面我们模拟一一下dubbo的标签实现
1.添加POM依赖
<properties> <springframework.version>4.0.0.RELEASE</springframework.version> </properties> <dependencies> <!-- **************************** Spring 依赖 **************************** --> <!-- 添加Spring-core包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${springframework.version}</version> </dependency> <!-- 添加spring-tx包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${springframework.version}</version> </dependency> <!-- Spring ORM 相关--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${springframework.version}</version> </dependency> <!-- 添加spring-jdbc包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${springframework.version}</version> </dependency> <!--添加spring-web包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${springframework.version}</version> </dependency> <!-- 添加spring-context包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${springframework.version}</version> </dependency> <!--添加aspectjweaver包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.5</version> </dependency> </dependencies>
2.创建xsd文件
在resource下创建文件xsd文件dubbo.xsd
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="http://liqiang/schema/dubbo" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" targetNamespace="http://liqiang/schema/dubbo" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:import namespace="http://www.springframework.org/schema/beans" /> <xsd:element name="server"><!--定义标签后缀名字--> <xsd:complexType> <xsd:complexContent> <xsd:extension base="beans:identifiedType"> <xsd:attribute name="interface" type="xsd:string" use="required"/><!--标签属性定义--> <xsd:attribute name="ref" type="xsd:string" use="required" /> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> </xsd:schema>
targetNamespace:为命名空间地址
3.创建自定义spring bean标签解析器
//定义自定义的scheme解析器 public class SimpleDateFormatBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { protected Class getBeanClass(Element element) { return DubboConfig.class; } protected void doParse(Element element, BeanDefinitionBuilder bean) { String interfaceFullName = element.getAttribute("interface"); String refName = element.getAttribute("ref"); if (StringUtils.hasText(interfaceFullName)) { bean.addPropertyValue("interfaceFullName", interfaceFullName); } if (StringUtils.hasText(refName)) { bean.addPropertyValue("refName", refName); } } }
4.注册自定义解析器Handle
/** * 注册自定义scheme解析器 */ public class MyNamespaceHandler extends NamespaceHandlerSupport { public void init() { //必须是elementName registerBeanDefinitionParser("server", new SimpleDateFormatBeanDefinitionParser()); } }
5.注册之定义Handle与spring产生关联
1.在resource目录下创建META-INF目录并创建spring.handlers和spring.schemas文件
spring.handlers内容
指向 自定义解析注册handle类的fullname
http\://liqiang/schema/dubbo=com.liqiang.test.MyNamespaceHandler
spring.schemas
http\://liqiang/schema/dubbo/dubbo.xsd=dubbo.xsd
6.创建spring配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://liqiang/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://liqiang/schema/dubbo http://liqiang/schema/dubbo/dubbo.xsd"> <dubbo:server id="test" interface="com.liqiang.userService" ref="userServiceImpl"></dubbo:server> </beans>
留意加粗备份为你自定义标签的命名空间和xsd
7.测试
public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-application.xml"); DubboConfig dubboConfig = (DubboConfig) ctx.getBean("test"); System.out.println(dubboConfig.getInterfaceFullName()+"|"+dubboConfig.getRefName()); }
输出
Dubbo对自定义Schame实现
我们知道 自定义Schame 需要定义解析器,和注册解析的handle 所以我们直接看dubbo对应的类就行了
DubboNamespaceHandler
public void init() { this.registerBeanDefinitionParser("application", new DubboBeanDefinitionParser(ApplicationConfig.class, true)); this.registerBeanDefinitionParser("module", new DubboBeanDefinitionParser(ModuleConfig.class, true)); this.registerBeanDefinitionParser("registry", new DubboBeanDefinitionParser(RegistryConfig.class, true)); this.registerBeanDefinitionParser("monitor", new DubboBeanDefinitionParser(MonitorConfig.class, true)); this.registerBeanDefinitionParser("provider", new DubboBeanDefinitionParser(ProviderConfig.class, true)); this.registerBeanDefinitionParser("consumer", new DubboBeanDefinitionParser(ConsumerConfig.class, true)); this.registerBeanDefinitionParser("protocol", new DubboBeanDefinitionParser(ProtocolConfig.class, true)); this.registerBeanDefinitionParser("service", new DubboBeanDefinitionParser(ServiceBean.class, true)); this.registerBeanDefinitionParser("reference", new DubboBeanDefinitionParser(ReferenceBean.class, false)); this.registerBeanDefinitionParser("annotation", new DubboBeanDefinitionParser(AnnotationBean.class, true)); }
dubbo所有标签都是交给DubboBeanDefinitionParser解析的。内部根据传入的class判断是什么标签。并用对应的Config内进行封装
DubboBeanDefinitionParser
public class DubboBeanDefinitionParser implements BeanDefinitionParser {public DubboBeanDefinitionParser(Class<?> beanClass, boolean required) { this.beanClass = beanClass; this.required = required; } public BeanDefinition parse(Element element, ParserContext parserContext) { return parse(element, parserContext, this.beanClass, this.required); } private static BeanDefinition parse(Element element, ParserContext parserContext, Class<?> beanClass, boolean required) { RootBeanDefinition beanDefinition = new RootBeanDefinition(); .........//省略部分代码 if (ProtocolConfig.class.equals(beanClass)) { String[] arr$ = parserContext.getRegistry().getBeanDefinitionNames(); len$ = arr$.length; for(int i$ = 0; i$ < len$; ++i$) { String name = arr$[i$]; BeanDefinition definition = parserContext.getRegistry().getBeanDefinition(name); PropertyValue property = definition.getPropertyValues().getPropertyValue("protocol"); if (property != null) { Object value = property.getValue(); if (value instanceof ProtocolConfig && id.equals(((ProtocolConfig)value).getName())) { definition.getPropertyValues().addPropertyValue("protocol", new RuntimeBeanReference(id)); } } } } else if (ServiceBean.class.equals(beanClass)) { className = element.getAttribute("class"); if (className != null && className.length() > 0) { RootBeanDefinition classDefinition = new RootBeanDefinition(); classDefinition.setBeanClass(ReflectUtils.forName(className)); classDefinition.setLazyInit(false); parseProperties(element.getChildNodes(), classDefinition); beanDefinition.getPropertyValues().addPropertyValue("ref", new BeanDefinitionHolder(classDefinition, id + "Impl")); } } else if (ProviderConfig.class.equals(beanClass)) { parseNested(element, parserContext, ServiceBean.class, true, "service", "provider", id, beanDefinition); } else if (ConsumerConfig.class.equals(beanClass)) { parseNested(element, parserContext, ReferenceBean.class, false, "reference", "consumer", id, beanDefinition); }
.......//省略部分代码
}
注意 上面并没有立刻创建对应的对象到spring容器 而是通过BeanDefinition 进行封装 class 哪些参数等。。将beanDefinition交于spring框架初始化