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