一、首先建立一个自主定义的标签xsd文件,可以放入自己想定义的任意名称的标签
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="http://www.mahongb.com/schema/soa" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.mahongb.com/schema/soa" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:element name="myTest"> <xsd:complexType> <xsd:attribute name="test" type="xsd:string"></xsd:attribute> </xsd:complexType> </xsd:element> <xsd:element name="service"> <xsd:complexType> <xsd:attribute name="interface" type="xsd:string"></xsd:attribute> <xsd:attribute name="ref" type="xsd:string"></xsd:attribute> </xsd:complexType> </xsd:element> </xsd:schema>
此处定义标签myTest,里面有子标签test,类型为String类型;定义标签service,里面有子标签interface与ref,类型都是String
二、定义标签实体类,接收标签元素与其子元素
package cn.spring.configBean; public class Mytest { private String test; public String getTest() { return test; } public void setTest(String test) { this.test = test; } }
package cn.spring.configBean; public class Service { private String inter; private String ref; public String getInter() { return inter; } public void setInter(String inter) { this.inter = inter; } public String getRef() { return ref; } public void setRef(String ref) { this.ref = ref; } }
以自定义的标签为名生成标签类,其中的子标签为类中的元素,类型为标签中的类型
三、建立处理标签的类,主要用来进行标签的解释,即标签与实体类对应起来
package cn.spring.parse; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; import cn.spring.configBean.Mytest; import cn.spring.configBean.Service; public class SOANamespaceHandler extends NamespaceHandlerSupport { public void init() { registerBeanDefinitionParser("myTest", new MytestBeanDefinitionParse(Mytest.class)); registerBeanDefinitionParser("service", new ServiceBeanDefinitionParse(Service.class)); } }
此处的解释类需要继承 NamespaceHandlerSupport 这个类,实现init方法完成标签的解释
三、建立解析自定义标签的类
package cn.spring.parse; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.xml.BeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.w3c.dom.Element; public class MytestBeanDefinitionParse implements BeanDefinitionParser { private Class<?> beanclass; public MytestBeanDefinitionParse(Class<?> beanclass){ this.beanclass = beanclass; } public BeanDefinition parse(Element arg0, ParserContext arg1) { RootBeanDefinition beanDefinition = new RootBeanDefinition(); beanDefinition.setBeanClass(beanclass); beanDefinition.setLazyInit(false); String test = arg0.getAttribute("test"); if(test == null || "".equals(test)){ throw new RuntimeException("test不能为null"); } beanDefinition.getPropertyValues().addPropertyValue("test", test); return beanDefinition; } }
进行标签 mytest 的解析
package cn.spring.parse; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.xml.BeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.w3c.dom.Element; public class ServiceBeanDefinitionParse implements BeanDefinitionParser { private Class<?> beanclass; public ServiceBeanDefinitionParse(Class<?> beanclass){ this.beanclass = beanclass; } public BeanDefinition parse(Element arg0, ParserContext arg1) { RootBeanDefinition beanDefinition = new RootBeanDefinition(); beanDefinition.setBeanClass(beanclass); beanDefinition.setLazyInit(false); String inter = arg0.getAttribute("interface"); String ref = arg0.getAttribute("ref"); if(inter == null || "".equals(inter)){ throw new RuntimeException("interface不能为空"); } if(ref == null || "".equals(ref)){ throw new RuntimeException("ref不能为空"); } beanDefinition.getPropertyValues().addPropertyValue("inter", inter); beanDefinition.getPropertyValues().addPropertyValue("ref", ref); return beanDefinition; } }
进行标签 service 的解析
四、spring读取我们自定义的schema和handlers默认是根据spring.handlers和spring.schemas来读取的,这两个文件默认要放在META-INF下
http\://www.dongnaoedu.com/schema/soa.xsd=META-INF/soa.xsd
spring.schemas 文件使用指定路径下的文件 META-INF/soa.xsd 来生成自定义标签
http\://www.dongnaoedu.com/schema/soa=cn.spring.parse.SOANamespaceHandler
spring.handlers 文件使用指定包名下的类 SOANamespaceHandler 来进行标签类的解释,与实体类对应起来
五、建立xml文件进行检验
<?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:mhb="http://www.mahongb.com/schema/soa" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.dongnaoedu.com/schema/soa http://www.dongnaoedu.com/schema/soa.xsd" default-lazy-init="true"> <bean id="userSreviceImp" class="cn.spring.test.UserServiceImp"></bean> <mhb:service interface="cn.spring.test.UserService" ref="userSreviceImp"></mhb:service> <mhb:myTest test="mhb"></mhb:myTest> </beans>