spring中编写配置可以用两种方式:
- 普通的通过 <bean id="" class=""><property name="" ref=""></bean> 这种默认标签配置方式
- 自定义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:tsearcher="http://www.taobao.com/terminator/tsearcher" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.taobao.com/terminator/tsearcher http://www.taobao.com/terminator/tsearcher.xsd"> <tsearcher:parent id="testparent"> <tsearcher:child /> </tsearcher:parent> </beans>
这种自定义的配置方式,在spring的实现中已经有部门实现,分别在几个命令空间中详细说明请查看(http://static.springsource.org/spring/docs/2.0.x/reference/xsd-config.html)
下面通过一个例子来说明一下,如何实现spring 自定义bean的方式来配置对象:
首先,定义两个properties文件,
- 一个是 spring.handlers 这个文件的作用是将配置文件中的命名空间与处理命名空间的handler(NamespaceHandlerSupport)联系起来,
- 另外一个文件是spring.schemas 文件,这个文件的作用是定义xsd文件的虚拟路径
spring.handlers例子:
http\://www.taobao.com/terminator/tsearcher=com.taobao.terminator.tag.TermiantorTSearcherNamespaceHandler
spring.shcemas例子:
http\://www.taobao.com/terminator/tsearcher.xsd=com/taobao/terminator/xsd/tsearcher.xsd
接下来要写一个namespaceHandler 类,用来为这个名称空间下的每个标签定义解析器。
例如,上面提到的TermiantorTSearcherNamespaceHandler:
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
public class TermiantorTSearcherNamespaceHandler extends
NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionParser("parent", new ParentBeanParser());
registerBeanDefinitionParser("child", new ChildParser());
}
}
init方法中调用了两次registerBeanDefinitionParser,申明了parent,child 标签的解析器。
parent标签和child标签的关系是父子关系,spring配置文件如下:
<?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:tsearcher="http://www.taobao.com/terminator/tsearcher" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.taobao.com/terminator/tsearcher http://www.taobao.com/terminator/tsearcher.xsd"> <tsearcher:parent id="testparent"> <tsearcher:child /> </tsearcher:parent> </beans>
定义tsearcher.xsd的xml元素结构信息:
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="http://www.taobao.com/terminator/tsearcher" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" targetNamespace="http://www.taobao.com/terminator/tsearcher" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:import namespace="http://www.springframework.org/schema/beans" /> <xsd:element name="parent"> <xsd:complexType> <xsd:complexContent> <xsd:extension base="beans:identifiedType"> <xsd:sequence> <xsd:element ref="child" /> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> <xsd:element name="child"> <xsd:complexType> <xsd:complexContent> <xsd:extension base="beans:identifiedType"> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> </xsd:schema>
这里最重要的是parent标签的解析器ParentBeanParser,在doParse方法中,还需要启动子标签child的解析流程,不然的话子标签child不会被解析:
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
/**
* @author 百岁(baisui@taobao.com)
* @date 2013-3-15
*/
public class ParentBeanParser extends AbstractSimpleBeanDefinitionParser {
@Override
protected void doParse(Element element, ParserContext parserContext,
BeanDefinitionBuilder builder) {
super.doParse(element, parserContext, builder);
builder.addPropertyValue("child", parserContext.getDelegate()
.parseCustomElement(
DomUtils.getChildElementByTagName(element, "child"),
builder.getRawBeanDefinition()));
}
@Override
protected Class<Parent> getBeanClass(Element element) {
return Parent.class;
}
}
这里特别要说明的是,在调用parserContext.getDelegate()
.parseCustomElement(DomUtils.getChildElementByTagName(element, "child"), builder.getRawBeanDefinition())
方法的时候,方法parseCustomElement的第二个beanDefinition参数是必须的,不然的话框架会认为这个元素是根结点元素,必须要有一个id属性。
接下来又出现一个新的需求,如果parent和child是一对多关系,例如标签格式如下:
<tsearcher:parent id="testparent"> <tsearcher:child name="1"/> <tsearcher:child name="2"/> <tsearcher:child name="3"/> </tsearcher:parent>
显然,用上面介绍的ParentBeanParser这个类中的解析标签的方式是不能满足需求的。
如果要知道如何解决一对多的关系请查阅下一篇博客(http://mozhenghua.iteye.com/blog/1914155)
结束