spring的自定义配置

完成一个spring的自定义配置一般需要以下5个步骤:

  1. 设计配置属性和JavaBean
  2. 编写XSD文件 全称就是 XML Schema 它就是校验XML,定义了一些列的语法来规范XML
  3. 编写NamespaceHandler和BeanDefinitionParser完成解析工作
  4. 编写spring.handlers和spring.schemas串联起所有部件
  5. 在Bean文件中应用

整体目录:
这里写图片描述

1、设计配置属性和JavaBean

先配置属性MyName.xml,可以看出一个people对象,有三个属性,id、name和age

<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:jesper="http://study.163.com/u/ykt1461577211836/schema/people"  
    xsi:schemaLocation="  
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
http://study.163.com/u/ykt1461577211836/schema/people http://study.163.com/u/ykt1461577211836/schema/people.xsd">  


    <jesper:people id="jesper" name="在云端" age="18"/>

</beans> 

对应的JavaBean

public class People {
    private String id;  
    private String name;  
    private Integer age;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }  


}

2、编写XSD文件 全称就是 XML Schema 它就是校验XML,定义了一些列的语法来规范XML

下面就是这个项目的xsd文件people.xsd

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

从上面可以看出,限制name属性是string类型、age属性是int类

这里写图片描述
我们试图去将配置文件的age改为string,立马提示报错

3、编写NamespaceHandler和BeanDefinitionParser完成解析工作

NamespaceHandler这个类是个处理器,MyNamespaceHandler 类中的init()方法是通过把people注册到解析器PeopleBeanDefinitionParser里面

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

public class MyNamespaceHandler extends NamespaceHandlerSupport {
    public void init() {
        registerBeanDefinitionParser("people", new PeopleBeanDefinitionParser());
    }
}

PeopleBeanDefinitionParser.java

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 PeopleBeanDefinitionParser extends
        AbstractSingleBeanDefinitionParser {
    protected Class getBeanClass(Element element) {
        return People.class;
    }

    protected void doParse(Element element, BeanDefinitionBuilder bean) {
        String name = element.getAttribute("name");
        String age = element.getAttribute("jesper");
        String id = element.getAttribute("id");
        if (StringUtils.hasText(id)) {
            bean.addPropertyValue("id", id);
        }
        if (StringUtils.hasText(name)) {
            bean.addPropertyValue("name", name);
        }
        if (StringUtils.hasText(age)) {
            bean.addPropertyValue("age", Integer.valueOf(age));
        }
    }
}

解析器作用:从配置文件中通过element.getAttribute(String name)拿到属性的值,赋给bean。

那么问题来了,people.xsd和MyNamespaceHandler 、PeopleBeanDefinitionParser 这两个类是关联起来呢?第四步来解决这个题

4、编写spring.handlers和spring.schemas串联起所有部件

spring.handlers

http\://study.163.com/u/ykt1461577211836/schema/people=com.soa.other.spring.schema.MyNamespaceHandler

spring.schemas

http\://study.163.com/u/ykt1461577211836/schema/people.xsd=META-INF/people.xsd

5、 在Bean文件中应用

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.soa.other.spring.schema.People;

public class TestSchema {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/xml/application.xml");  
        People p = (People)ctx.getBean("jesper");
        System.out.println(p.getId());  
        System.out.println(p.getName());  
        System.out.println(p.getAge()); 
    }
}

运行结果
这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值