Spring框架的XML扩展特性
Spring框架从2.0版本开始,提供了基于Schema风格的XML扩展机制,允许开发者扩展spring配置文件。现在我们来看下怎么实现这个功能,可以参考spring帮助文档中的《Extensible XML authoring》。
我们知道如果在需要在spring.xml中配置数据源,需要进行如下的配置:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3309/sampledb" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean>
这种方式配置虽然也比较简单,但是有一个缺点:使用<property>标签不够明显,不如元素属性那么直接。现在我们希望在spring.xml中做如下的配置,就能够完成数据源的配置。
<myns:datasource id="myDataSourcce"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3309/demodb"
userName="root"
password="root" />
如果让spring能够解析这个标签,需要4步。
1、提供一个xsd文件,负责对xml的标签<datasource>进行校验。
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.aty.com/schema/aty"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://www.mycompany.com/schema/myns"
elementFormDefault="qualified"
attributeFormDefault="unqualified"> <xsd:import namespace="http://www.springframework.org/schema/beans" /> <xsd:element name="datasource">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="driverClassName" type="xsd:string" use="required" />
<xsd:attribute name="url" type="xsd:string" use="required" />
<xsd:attribute name="username" type="xsd:string" use="required" />
<xsd:attribute name="password" type="xsd:string" use="required" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
2、定义一个BeanDefinitionParser负责解析xml,并将必要的信息放入spring中。
package net.mingyang.spring_extensible_xml; import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.w3c.dom.Element; public class DatasourceBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { protected Class getBeanClass(Element element) {
return BasicDataSource.class;
} protected void doParse(Element element, BeanDefinitionBuilder bean) {
String driverClassName = element.getAttribute("driverClassName");
bean.addPropertyValue("driverClassName", driverClassName); String url = element.getAttribute("url");
bean.addPropertyValue("url", url); String username = element.getAttribute("username");
bean.addPropertyValue("username", username); String password = element.getAttribute("password");
bean.addPropertyValue("password", password);
}
}
3、定义个NamespaceHandler, 由sping框架的调用入口。
package net.mingyang.spring_extensible_xml; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; public class DatasourceNamespaceHandlerSupport extends NamespaceHandlerSupport
{
public void init()
{
registerBeanDefinitionParser("datasource",
new DatasourceBeanDefinitionParser());
}
}
4、配置schema和handler。我们通过一些配置文件来告知Spring,它们就是spring.handlers和spring.schemas,它们放在META-INF目录中。
spring.handlers内容如下:
http\://www.mycompany.com/schema/myns=net.mingyang.spring_extensible_xml.DatasourceNamespaceHandlerSupport
spring.schemas内容如下:
http\://www.mycompany.com/schema/myns/myns.xsd=META-INF/myns.xsd
OK,到这里Spring需要的所有定义就完了。
测试类:
package net.mingyang.spring_extensible_xml; import javax.sql.DataSource; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DatasourceXmlTest
{
public static void main(String[] args)
{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"spring.xml");
DataSource ds = (DataSource) context.getBean("myDataSourcce");
System.out.println(ds);
}
}
spring.xml内容:
<?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:myns="http://www.mycompany.com/schema/myns"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.mycompany.com/schema/myns
http://www.mycompany.com/schema/myns/myns.xsd"> <myns:datasource id="myDataSourcce"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3309/demodb"
username="root"
password="root" /> </beans>
Spring框架的XML扩展特性的更多相关文章
- spring 框架的xml文件如何读取properties文件数据
spring 框架的xml文件如何读取properties文件数据 第一步:在spring配置文件中 注意:value可以多配置几个properties文件 <bean id="pro ...
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...
- java代码和spring框架读取xml和properties文件
1.java文件读取properties文件 Properties props = new Properties(); try { //资源文件存放在类文件的根目录下.即是放在src下面.则不需要写路 ...
- 配置Spring框架编写XML的提示
1. 步骤一:先复制, http://www.springframework.org/schema/beans/spring-beans.xsd 2. 步骤二:搜索XML Catalog,点击Add按 ...
- 【常用配置】Spring框架web.xml通用配置
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java. ...
- spring框架 事务 xml配置方式
user=LF password=LF jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl driverClass=oracle.jdbc.driver.Ora ...
- Spring框架——基于XML/注解开发
IoC的实现方式有两种:XML配置文件.基于注解. MVC开发模式: Controller层 Service层 Repository层 Controller层调用Service,Service调用Re ...
- Spring框架配置beans.xml
Spring学习笔记(一) 一.Spring 框架 Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 ...
- spring框架篇(一)-------spring简介与配置文件使用控制反转事例
spring简介 Spring 是一个开源框架,中文意思就是春天,也许是作者想让自己的这个框架给Java开发人员带来春天吧.其官方网站是 https://spring.io/ ,可以在官方网站下载到完 ...
随机推荐
- python 抓取百度音乐
# coding:utf-8 import urllib2 import re import urllib import chardet from json import * category = ' ...
- 基于Maven site的穷人的本地知识管理系统
1 Motivation On daily study or development, a simple knowledge management system is required. In the ...
- jQuery插件开发的两种方法及$.fn.extend的详解
jQuery插件开发分为两种: 1 类级别 类级别你可以理解为拓展jquery类,最明显的例子是$.ajax(...),相当于静态方法. 开发扩展其方法时使用$.extend方法,即jQuery.ex ...
- Shell基础-环境变量配置文件
Shell基础-环境变量配置文件 source 配置文件 或者 . 配置文件: 把环境变量写入配置文件后,需要用户重新登陆才能生效,而是用source命令,则能直接生效 主要的配置文件: /etc/p ...
- JSP代码加固
String id = request.getParameter("id"); String id = id.replace("'","") ...
- DOm4解析xml
1.创建XML文档对象的的方式有两种 1)Document document=DocumentHelper.createDocument(); 2)DocumentFactory documentFa ...
- 灰色蓝色系简洁自适应登录HTML页面
自己写了一个简介的登录页面,页面背景为灰色,居中为登录框,登录面板为半透明效果,整体十分美观,登录按钮有JS效果.自己感觉还不错,拿出来分享一下. 页面效果图 源码下载链接:http://files. ...
- Unicode 和 UTF-8 的关系
曾经这个世界上,有着gb2312,gbk,latin1,utf 等各种字符集,现在,我们也能不时的看到他们的身影. 但是值得庆幸的事,时过境迁,这些主要的字符集,都已经逐渐被utf8取代. 但是我们很 ...
- malloc分配的内存空间是连续的吗
1.linux内核管理内存空间的分配,所有程序对内存空间的申请和其他操作,最终都会交给内核来管理. 2.linux实现的是“虚拟内存系统”,对用户而言,所有内存都是虚拟的,也就是说程序并不是直接运行在 ...
- etcd第一集
网站:https://github.com/coreos/etcd 一些观点:https://yq.aliyun.com/articles/11035 1.etcd是键值存储仓库,配置共享和服务发现2 ...