package net.itaem.vo; public class User { private String name; private String sex; private String email; private String id; //set get method... }
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.lexueba.com/schema/user" xmlns:tns="http://www.lexueba.com/schema/user" elementFormDefault="qualified"> <element name="user"> <complexType> <attribute name="id" type="string" /> <attribute name="name" type="string" /> <attribute name="sex" type="string" /> <attribute name="email" type="string" /> </complexType> </element> </schema>
/* * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.itaem.parser; import net.itaem.vo.User; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.util.StringUtils; import org.w3c.dom.Element; /** * * @author Administrator */ public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser{ /* (non-Javadoc) * @see org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser#getBeanClass(org.w3c.dom.Element) */ @Override protected Class<?> getBeanClass(Element element) { return User.class; } //从elelment中解析并提取对应的元素 @Override protected void doParse(Element element, BeanDefinitionBuilder builder) { String name=element.getAttribute("name"); String email=element.getAttribute("sex"); String sex=element.getAttribute("sex"); //将提取到的数据放入beanDefinitionBuilder 中,待完成所有的bean解析后统一放到beanfactory if(StringUtils.hasText(name)){ builder.addPropertyValue("name", name); } if(StringUtils.hasText(email)){ builder.addPropertyValue("email", email); } if(StringUtils.hasText(sex)){ builder.addPropertyValue("sex", sex); } } }
/* * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.itaem.handler; import net.itaem.parser.UserBeanDefinitionParser; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; /** * * @author Administrator */ public class MyNamespaceHandler extends NamespaceHandlerSupport { /* (non-Javadoc) * @see org.springframework.beans.factory.xml.NamespaceHandler#init() */ @Override public void init() { registerBeanDefinitionParser("user", new UserBeanDefinitionParser()); } }
修改spring.handlers 文件,添加以下内容
http\://www.lexueba.com/schema/user=net.itaem.handler.MyNamespaceHandler
修改spring.schemas文件的内容,添加以下内容
http\://www.lexueba.com/schema/user.xsd=org/springframework/beans/factory/xml/use.xsd
<?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:myname="http://www.lexueba.com/schema/user" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.lexueba.com/schema/user http://www.lexueba.com/schema/user.xsd"> <myname:user id="beantest" name="whx" email="[email protected]" sex="男"/> </beans>
package net.itaem.test; import net.itaem.vo.User; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("net/itaem/source/custom_user.xml"); User bt=(User) context.getBean("beantest"); System.out.println(bt); } }