首先写一个pojo类user来接收配置文件
public class User {
private String userName;
private String emal;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmal() {
return emal;
}
public void setEmal(String emal) {
this.emal = emal;
}
}
定义一个xsd文件描述组件的内容 user.xsd
targetNamespace="http://www.ry.com/schema/user"意思是命名空间为 http://www.ry.com/schema/user
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.ryazmm.com/schema/user"
xmlns:tns="http://www.ranyang.com/schema/user"
elementFormDefault="qualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="user">
<xsd:complexType>
<xsd:attribute name="id" type="string" />
<xsd:attribute name="userName" type="string" />
<xsd:attribute name="emal" type="string" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
创建一个文件解析xsd文件和组件的定义
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.w3c.dom.Element;
import org.springframework.util.StringUtils;
public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser{
protected Class getBeanClass(Element element)
{
return User.class;
}
protected void doParse(Element element,BeanDefinitionBuilder bean){
String userName=element.getAttribute("userName");
String emal=element.getAttribute("emal");
if(StringUtils.hasText(userName))
{
bean.addPropertyValue("userName",userName);
}
if(StringUtils.hasText(emal))
{
bean.addPropertyValue("emal",emal);
}
}
}
创建一个handler文件为了把组件注册到spring容器
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
public class MyNamespaceHandler extends NamespaceHandlerSupport{
@Override
public void init() {
// TODO Auto-generated method stub
registerBeanDefinitionParser("user",new UserBeanDefinitionParser());
}
}
下面重要的一点是编写映射文件
spring.handlers内容为
http\://www.ry.com/schema/user=com.fhit.util.MyNamespaceHandler 意思是当遇到以user为开头的bean时,进入方法MyNamespaceHandler
spring.schemas内容为
http\://www.ry.com/schema/user.xsd=META-INF/user.xsd 意思是引用的www.ry.com/schema/user.xsd指向本地META-INF/user.xsd
创建测试配置文件test.xml
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:myname="http://www.ranyang.com/schema/user"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.ranyang.com/schema/user
http://www.ranyang.com/schema/user.xsd">
<myname:user id="testbean" userName="aa" emal="bb"/>
</beans>
最终测试
public static void main(string[] args){
//通过映射加载test.xml 遇到bean<myname:user时依据user.xsd进行解析,通过spring.handlers映射到
//MyNamespaceHandler再进入UserBeanDefinitionParser()加入到beanFactory中
ApplicationContext bf=new ClassPathXmlApplicationContext("com/fhit/util/test.xml");
User user=(User) bf.getBean("testbean");
system.out.print(user.getUserName()+","+user.getEmal());
}
输出为aa,bb