1.引入
很多情况下,我们需要为系统提供可配置化支持,简单的做法就是可以直接基于spring的bean来配置,但配置较为复杂或者需要更丰富控制的时候,会显得很笨拙
一般的做法会用原生态的方法去解析定义好的xml文件,然后转化为配置对象,这种方式当然可以解决所有问题,但是实现起来比较繁琐,特别是在配置非常复杂的时候,解析工作也是一个不得不考虑的负担
spring提供了可扩展Schema的支持,这时一个不错的折中方案
2.步骤
完成一个自定义配置需要以下步骤:
(1)设计配置属性和javaBean
(2)编写xsd文件
(3)编写NamespaceHander和BeanDefinitionParser完成解析工作
(4)编写spring.handlers和spring.schemas串联起所有组件
(5)在bean文件中应用
3.实例
(1) 首先需要设计好配置项
public class Person
{
private String id;
private String name;
private Integer age;
}
(2) 为上一步设计好的配置项编写xsd文件,xsd是schema的定义文件,配置的输入和解析输出都是以xsd为契约
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
xmlns="http://www.yihaomen.com/schema/people"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://www.yihaomen.com/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>
完成后,需要把这个xsd存放在classpath下,一般都放在META-INF目录下
(3)编写NamespaceHandler和BeanDefinitionParser完成解析工作
NamespaceHandler会根据schema和节点名找到某个BeanDefinitionParser,然后由BeanDefinitionParser完成具体的解析工作。(实际上NamespaceHandler就是将节点和BeanDefinitionParser对应起来即可)
需要分别完成NamespaceHandler和BeanDefinitionParser的实现类,Spring提供了默认实现类NamespaceHandlerSupport和AbstractSingleBeanDefinitionParser,简单的方式就是去继承这两个类。
public class MyNamespaceHandler extends NamespaceHandlerSupport
{
public void init()
{
registerBeanDefinitionParser("people", new PeopleBeanDefinitionParser());
}
}
其中registerBeanDefinitionParser("people", new PeopleBeanDefinitionParser());就是用来把节点名和解析类联系起来,在配置中引用people配置项时,就会用PeopleBeanDefinitionParser来解析配置。
public class PeopleBeanDefinitionParser extends AbstractSingleBeanDefinitionParser
{
protected Class<?> getBeanClass(Element e)
{
return People.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(age))
{
bean.addPropertyValue("age", age);
}
if(StringUtils.hasText(name))
{
bean.addPropertyValue("name", name);
}
}
}
其中,element.getAttribute就是获得配置中的属性值,bean.addPropertyValue就是把属性值放入bean中
(4)编写spring.handlers和spring.schemas串联起所有部件
上面几个步骤走下来会发现开发好的handler和xsd还没法让应用感知到,spring提供了spring.handlers和spring.schemas这两个配置文件来完成这项工作,这两个文件需要我们自己编写并放入META-INF文件夹中,这两个文件的地址必须是META-INF/spring.handlers和META-INF/spring.schemas,spring会默认去载入它们
spring.handlers:
http\://www.yihaomen.com/schema/people=com.springwork.xml.MyNamespaceHandler
以上表示,当使用到名为“http\://blog.youkuaiyun.com/my/schema/people”的schema引用时,会通过com.mycompany.app.MyNamespaceHandler来完成节点和解析类的关联
spring.schemas如下所示:
http\://www.yihaomen.com/schema/people.xsd=META-INF/people.xsd
这就是载入xsd文件
(5)在bean文件中使用
使用方法和普通的spring bean类似,只不过需要基于我们自定义的schema
<?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:comtest="http://www.yihaomen.com/schema/people"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.yihaomen.com/schema/people http://www.yihaomen.com/schema/people.xsd ">
<comtest:people id="cutesource" name="一号门" age="1024"/>
</beans>
其中xmlns:comtest="http://www.yihaomen.com/schema/people" ,是用来指定自定义的schema,
xsi:schemaLocation用来指定xsd文件
(6)测试
public static void main( String[] args )
{
String[] locations = {"beans.xml"};
ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);
People p = (People) ctx.getBean("cutesource");
System.out.println("name is "+p.getName());
}