项目结构图
代码
application.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:people="http://www.laiwenqiang.com/spring/schema/people"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.laiwenqiang.com/spring/schema/people http://www.laiwenqiang.com/spring/schema/people.xsd" >
<people:student id="student1" name="student1"
age="18" />
</beans>
people.xsd
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns="http://www.laiwenqiang.com/spring/schema/people"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://www.laiwenqiang.com/spring/schema/people"
elementFormDefault="qualified"
attributeFormDefault="unqualified" >
<xsd:import namespace="http://www.springframework.org/schema/beans" />
<xsd:element name="student" >
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType" >
<xsd:attribute name="name"
type="xsd:string" >
<xsd:annotation>
<xsd:documentation>
姓名
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="age"
type="xsd:string" >
<xsd:annotation>
<xsd:documentation>
年龄
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
spring.handlers
http\:
spring.schemas
http\:
PeopleBeanDefinitionParser.java
package com.laiwenqiang.spring.sechema;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
* Created by laiwenqiang on 2017/1/29.
*/
public class PeopleBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
protected Class getBeanClass (Element element) {
return Student.class;
}
protected void doParse (Element element, BeanDefinitionBuilder bean) {
String name = element.getAttribute("name" );
String age = element.getAttribute("age" );
String id = element.getAttribute("id" );
if (StringUtils.hasText(id)) {
bean.addPropertyValue("id" , id);
}
if (StringUtils.hasText(name)) {
bean.addPropertyValue("name" , name);
}
if (StringUtils.hasText(age)) {
bean.addPropertyValue("age" , Integer.valueOf(age));
}
}
}
student.java
package com.laiwenqiang.spring.sechema;
/**
* Created by laiwenqiang on 2017/1/29.
*/
public class Student {
private String id;
private String name;
private String age;
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 getAge () {
return age;
}
public void setAge (String age) {
this .age = age;
}
}
StudentNamespaceHandler.java
package com.laiwenqiang.spring.sechema;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
/**
* Created by laiwenqiang on 2017/1/29.
*/
public class StudentNamespaceHandler extends NamespaceHandlerSupport {
public void init () {
registerBeanDefinitionParser("student" , new PeopleBeanDefinitionParser());
}
}
Main.java
package com.laiwenqiang.spring.sechema;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by laiwenqiang on 2017/1/29.
*/
public class Main {
public static void main (String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml" );
Student people = (Student) ctx.getBean("student1" );
System.out.println(people.getId());
System.out.println(people.getName());
System.out.println(people.getAge());
}
}