完成一个spring的自定义配置一般需要以下5个步骤:
- 设计配置属性和JavaBean
- 编写XSD文件 全称就是 XML Schema 它就是校验XML,定义了一些列的语法来规范XML
- 编写NamespaceHandler和BeanDefinitionParser完成解析工作
- 编写spring.handlers和spring.schemas串联起所有部件
- 在Bean文件中应用
整体目录:
1、设计配置属性和JavaBean
先配置属性MyName.xml,可以看出一个people对象,有三个属性,id、name和age
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jesper="http://study.163.com/u/ykt1461577211836/schema/people"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://study.163.com/u/ykt1461577211836/schema/people http://study.163.com/u/ykt1461577211836/schema/people.xsd">
<jesper:people id="jesper" name="在云端" age="18"/>
</beans>
对应的JavaBean
public class People {
private String id;
private String name;
private Integer 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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
2、编写XSD文件 全称就是 XML Schema 它就是校验XML,定义了一些列的语法来规范XML
下面就是这个项目的xsd文件people.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
xmlns="http://study.163.com/u/ykt1461577211836/schema/people"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://study.163.com/u/ykt1461577211836/schema/people"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans" />
<xsd:element name="people">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="age" type="xsd:int" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
从上面可以看出,限制name属性是string类型、age属性是int类
我们试图去将配置文件的age改为string,立马提示报错
3、编写NamespaceHandler和BeanDefinitionParser完成解析工作
NamespaceHandler这个类是个处理器,MyNamespaceHandler 类中的init()方法是通过把people注册到解析器PeopleBeanDefinitionParser里面
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
public class MyNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
registerBeanDefinitionParser("people", new PeopleBeanDefinitionParser());
}
}
PeopleBeanDefinitionParser.java
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
public class PeopleBeanDefinitionParser extends
AbstractSingleBeanDefinitionParser {
protected Class getBeanClass(Element element) {
return People.class;
}
protected void doParse(Element element, BeanDefinitionBuilder bean) {
String name = element.getAttribute("name");
String age = element.getAttribute("jesper");
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));
}
}
}
解析器作用:从配置文件中通过element.getAttribute(String name)拿到属性的值,赋给bean。
那么问题来了,people.xsd和MyNamespaceHandler 、PeopleBeanDefinitionParser 这两个类是关联起来呢?第四步来解决这个题
4、编写spring.handlers和spring.schemas串联起所有部件
spring.handlers
http\://study.163.com/u/ykt1461577211836/schema/people=com.soa.other.spring.schema.MyNamespaceHandler
spring.schemas
http\://study.163.com/u/ykt1461577211836/schema/people.xsd=META-INF/people.xsd
5、 在Bean文件中应用
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.soa.other.spring.schema.People;
public class TestSchema {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/xml/application.xml");
People p = (People)ctx.getBean("jesper");
System.out.println(p.getId());
System.out.println(p.getName());
System.out.println(p.getAge());
}
}
运行结果