目录图:
引入jar包:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.12.RELEASE</version> </dependency>
1:Application类
package com.imddy.ttdubbo.springsupport; public class Application { private String id; private String name; private String version; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } @Override public String toString() { return "Application [id=" + id + ", name=" + name + ", version=" + version + "]"; } }
2:ApplicationBeanDefinitionParser类
package com.imddy.ttdubbo.springsupport; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.util.StringUtils; import org.w3c.dom.Element; public class ApplicationBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { private final Class<?> beanClass; private final boolean required; public ApplicationBeanDefinitionParser(Class<?> beanClass, boolean required) { this.beanClass = beanClass; this.required = required; } protected Class getBeanClass(Element element) { return Application.class; } protected void doParse(Element element, BeanDefinitionBuilder bean) { String id = element.getAttribute("id"); String name = element.getAttribute("name"); String version = element.getAttribute("version"); if (StringUtils.hasText(id)) { bean.addPropertyValue("id", id); } if (StringUtils.hasText(name)) { bean.addPropertyValue("name", name); } if (StringUtils.hasText(version)) { bean.addPropertyValue("version", version); } } }
3:Registry类
package com.imddy.ttdubbo.springsupport; public class Registry { private String id; private String address; private Integer port; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Integer getPort() { return port; } public void setPort(Integer port) { this.port = port; } @Override public String toString() { return "Registry [id=" + id + ", address=" + address + ", port=" + port + "]"; } }
4:RegistryBeanDefinitionParser类
package com.imddy.ttdubbo.springsupport; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.util.StringUtils; import org.w3c.dom.Element; public class RegistryBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { protected Class getBeanClass(Element element) { return Registry.class; } protected void doParse(Element element, BeanDefinitionBuilder bean) { String id = element.getAttribute("id"); String address = element.getAttribute("address"); String port = element.getAttribute("port"); if (StringUtils.hasText(id)) { bean.addPropertyValue("id", id); } if (StringUtils.hasText(address)) { bean.addPropertyValue("address", address); } if (StringUtils.hasText(port)) { bean.addPropertyValue("port", Integer.valueOf(port)); } } }
5:TtdubboNamespaceHandler类
package com.imddy.ttdubbo.springsupport; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; public class TtdubboNamespaceHandler extends NamespaceHandlerSupport{ public void init() { // TODO Auto-generated method stub registerBeanDefinitionParser("application", new ApplicationBeanDefinitionParser(Application.class, true)); registerBeanDefinitionParser("registry", new RegistryBeanDefinitionParser()); } }
6:resources目录下创建META-INF目录
7:spring.handlers
http\://ttdubbo.oschina.net/schema/ttdubbo=com.imddy.ttdubbo.springsupport.TtdubboNamespaceHandler
8:spring.schemas
http\://ttdubbo.oschina.net/schema/ttdubbo.xsd=META-INF/ttdubbo.xsd
9:ttdubbo.xsd (此文件名可根据 定义的标签起名)
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <xsd:schema xmlns="http://ttdubbo.oschina.net/schema/ttdubbo" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:tool="http://www.springframework.org/schema/tool" targetNamespace="http://ttdubbo.oschina.net/schema/ttdubbo"> <xsd:import namespace="http://www.w3.org/XML/1998/namespace"/> <xsd:complexType name="applicationType"> <xsd:attribute name="id" type="xsd:ID"> <xsd:annotation> <xsd:documentation><![CDATA[ The unique identifier for a bean. ]]></xsd:documentation> </xsd:annotation> </xsd:attribute> <xsd:attribute name="name" type="xsd:string" use="required"> <xsd:annotation> <xsd:documentation><![CDATA[ The application name. ]]></xsd:documentation> </xsd:annotation> </xsd:attribute> <xsd:attribute name="version" type="xsd:string"> <xsd:annotation> <xsd:documentation><![CDATA[ The application version. ]]></xsd:documentation> </xsd:annotation> </xsd:attribute> </xsd:complexType> <xsd:complexType name="registryType"> <xsd:attribute name="id" type="xsd:ID"> <xsd:annotation> <xsd:documentation><![CDATA[ The unique identifier for a bean. ]]></xsd:documentation> </xsd:annotation> </xsd:attribute> <xsd:attribute name="address" type="xsd:string" use="required"> <xsd:annotation> <xsd:documentation><![CDATA[ The registry address. ]]></xsd:documentation> </xsd:annotation> </xsd:attribute> <xsd:attribute name="port" type="xsd:string" use="optional"> <xsd:annotation> <xsd:documentation><![CDATA[ The registry default port. ]]></xsd:documentation> </xsd:annotation> </xsd:attribute> </xsd:complexType> <xsd:element name="application" type="applicationType"> <xsd:annotation> <xsd:documentation><![CDATA[ The application config ]]></xsd:documentation> </xsd:annotation> </xsd:element> <xsd:element name="registry" type="registryType"> <xsd:annotation> <xsd:documentation><![CDATA[ The registry config ]]></xsd:documentation> </xsd:annotation> </xsd:element> </xsd:schema>
10:开始测试:
创建applicationContext.xml配制文件,引入ttdubbo的Namespaces
<?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:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:util="http://www.springframework.org/schema/util" xmlns:ttdubbo="http://ttdubbo.oschina.net/schema/ttdubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd http://ttdubbo.oschina.net/schema/ttdubbo http://ttdubbo.oschina.net/schema/ttdubbo.xsd"> <ttdubbo:application id="app1" name="ttdubboTest" version="0.0.1"/> <ttdubbo:registry id="reg1" address="192.168.1.101" port="80" /> </beans>
12:main方法
package com.imddy.ttdubbo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.imddy.ttdubbo.springsupport.Application; import com.imddy.ttdubbo.springsupport.Registry; public class APP { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Application application1 = (Application) context.getBean("app1"); System.out.println(application1.toString()); Registry registry1 = (Registry) context.getBean("reg1"); System.out.println(registry1.toString()); System.out.println("Hello World!"); } }
注:如果不成功,需要手动引入一下
13:打包后其它项目可以直接使用