天天看點

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.handler

spring.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