spring schema自定义扩展

Posted 全力付出

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring schema自定义扩展相关的知识,希望对你有一定的参考价值。

spring schema自定义扩展

Spring 2.5在2.0的基于Schema的Bean配置的基础之上,再增加了扩展XML配置的机制。通过该机制,我们可以编写自己的Schema,并根据自定义的Schema用自定的标签配置Bean。要使用的Spring的扩展XML配置机制,也比较简单,有以下4个步骤:

  1. 编写自定义Schema文件;
  2. 编写自定义NamespaceHandler;
  3. 编写解析BeanDefinition的parser
  4. 在Spring中注册上述组建

Maven依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.2.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>3.2.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>3.2.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.2.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>3.2.4.RELEASE</version>
</dependency>

一、编写schema文件

参考:http://www.w3school.com.cn/schema/schema_elements_ref.asp , 如下people.xsd文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.pomelo.com/schema/people"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:beans="http://www.springframework.org/schema/beans"
            targetNamespace="http://www.pomelo.com/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>

二、编写自定义NamespaceHandler

package schema;

import org.springframework.beans.factory.xml.NamespaceHandlerSupport;

public class StudentNamespaceHandler extends NamespaceHandlerSupport 

    @Override
    public void init() 
        registerBeanDefinitionParser("student", new StudentBeanDefinitionParser());
    



三、编写BeanDefinition

package schema;

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 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));
        
    

实体类:

package schema;

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;  
      


四、注册schema组件

最后在META-INF目录下添加两个配置文件(spring.handlerspring.schema):

spring.handler配置如下:

http\\://www.pomelo.com/schema/people=schema.StudentNamespaceHandler

spring.schema配置如下:

http\\://www.pomelo.com/schema/people.xsd=META-INF/people.xsd

五、测试

新建applicationContext.xml放在clasapath下面:

<?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.pomelo.com/schema/people"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.pomelo.com/schema/people http://www.pomelo.com/schema/people.xsd">

    <people:student id="student1" name="student1" age="18"/>

    <people:student id="student2" name="student2" age="20" />


    <bean id="student3" class="schema.Student">
        <property name="name" value="student3"/>
        <property name="age" value="23"/>
    </bean>

</beans>

java调用:

package schema;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by zhengyong on 17/3/3.
 */
public class SchemaTest 

    public static void main(String[] args) 

        ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        Student student1 = (Student) ctx.getBean("student1");
        Student student2 = (Student) ctx.getBean("student2");
        Student student3 = (Student) ctx.getBean("student3");

        System.out.println("name: " + student1.getName() + " age :" + student1.getAge());
        System.out.println("name: " + student2.getName() + " age :" + student2.getAge());
        System.out.println("name: " + student3.getName() + " age :" + student3.getAge());
    

具体代码详见:https://github.com/zyongjava/pomelo/blob/master/src/main/resources/META-INF/people.xsd

以上是关于spring schema自定义扩展的主要内容,如果未能解决你的问题,请参考以下文章

Spring Schema扩展机制

基于Spring的可扩展Schema进行开发自定义配置标签支持

Spring面试高频题如何:自定义XML schema 扩展

扩展SpringBoot Schema配置Bean

扩展SpringBoot Schema配置Bean

Dubbo中对Spring配置标签扩展