扩展schema,定义自己的bean属性。。不错!
主要:
1,定义META-INF下.xsd文件,这里是people.xsd;定义spring.handlers;定义spring.schemas
2,定义namaspace解析类,这里是StudentNamespaceHandler
3,定义beanDefinition,这里是StudentBeanDefinitionParser
4,当然还有相关的javabean定义,这里是Student.java
详细代码:
people.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.luyee.com/bat/schema/people"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://www.luyee.com/bat/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\://www.luyee.com/bat/schema/people=com.luyee.bat.spring.StudentNamespaceHandler
spring.schemas
http\://www.luyee.com/bat/schema/people.xsd=META-INF/people.xsd
StudentNamespaceHandler和StudentBeanDefinitionParser
package com.luyee.bat.spring; import java.text.SimpleDateFormat; import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element; public class StudentNamespaceHandler extends NamespaceHandlerSupport { public void init() {
registerBeanDefinitionParser("student", new StudentBeanDefinitionParser());
} class StudentBeanDefinitionParser extends AbstractSingleBeanDefinitionParser{
protected Class getBeanClass(Element element) {
return Student.class;
} protected void doParse(Element element, BeanDefinitionBuilder bean) {
String name = element.getAttribute("name");
bean.addPropertyValue("name", name); String age = element.getAttribute("age");
if (StringUtils.hasText(age)) {
bean.addPropertyValue("age", Integer.valueOf(age));
}
}
}
}
JavaBean:Student
package com.luyee.bat.spring; public class Student { private String name; private int age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} }
测试:applicationContex.xml(people:student就好比bean)
<?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.luyee.com/bat/schema/people"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.luyee.com/bat/schema/people http://www.luyee.com/bat/schema/people.xsd" >
<people:student id="student1" name="student1"
age="18" /> <people:student id="student2" name="student2"
age="20" /> </beans>
StudentXsdTest.java
package com.luyee.bat.spring; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class StudentXsdTest { public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student1 = (Student) ctx.getBean("student1");
Student student2 = (Student) ctx.getBean("student2");
System.out.println("name: " +student1.getName()+" age :" + student1.getAge());
System.out.println("name: " +student2.getName()+" age :" + student2.getAge());
}
}
输出:
八月 13, 2013 8:50:50 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@46f7ba12: startup date [Tue Aug 13 20:50:50 CST 2013]; root of context hierarchy
八月 13, 2013 8:50:50 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
八月 13, 2013 8:50:52 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@25e9a396: defining beans [student1,student2]; root of factory hierarchy
name: student1 age :18
name: student2 age :20
最后贴个文件结构