汉莫拉比

(spring-第2回【IoC基础篇】)Spring的Schema,基于XML的配置

要深入了解Spring机制,首先需要知道Spring是怎样在IoC容器中装配Bean的。而了解这一点的前提是,要搞清楚Spring基于Schema的Xml配置方案。

 

在深入了解之前,必须要先明白几个标签的意思(我会逐步引导读者理解,刚开始的懵懂无所谓,读者自会渐入佳境。初极狭,才通人。复行数十步,豁然开朗。)。

  1. 什么是XML Schema?

  用来描述 XML文档的结构,也被简称为XSD(XML Schema Definition),是一些规则的集合。(方式:通过定义schema文件 如 spring-tx-3.0.xsd)

  2. xmlns?

  命名空间是W3C推荐标准提供的一种统一命名XML文档中的元素和属性的机制。

  3.xsd文件?

  使用XML w3c 标准命名空间中规定的元素和属性编写的以targetNamespace作为{目标命名空间}的XML文件,能够约束引入此{目标命名空间}定义的元素和属性的XML文件。

  4.targetNamespace?

  目标命名空间,它的主要作用是指明Schema定义的元素的命名空间。

亲,是不是蒙圈了?我举个栗子你感受一下,下面是一个note.xsd文件示例:

代码001 

1
<?xml version="1.0"?> 2 <xs:schema 3 xmlns:xs="http://www.w3.org/2001/XMLSchema" 4 targetNamespace="http://www.w3school.com.cn"> 5 <xs:element name="note"> 6 <xs:complexType> 7 <xs:sequence> 8 <xs:element name="to" type="xs:string"/> 9 <xs:element name="from" type="xs:string"/> 10 <xs:element name="heading" type="xs:string"/> 11 <xs:element name="body" type="xs:string"/> 12 </xs:sequence> 13 </xs:complexType> 14 </xs:element> 15 </xs:schema>

这个xsd文件你就认为它是个有约束力的文件,它将约束后面定义的与bean相关的xml,

第3行的xmlns就是一个统一的规则,它就是"法",平民百姓可以自由支配自己,但必须遵循这个"法"的管制。

5-14行就是约束的方法,第五行"<"后面的"xs"是这个约束的小名,对应着第3行"xmlns"后面的"xs",而约束文件的全名就是第三行后面的以http开始的链接。(这个全名其实可以随意定义,但是一般情况下用网站目录来命名,一是便于区分,二是体现出本文件在服务器中的组织架构关系)。

第4行是本约束规则要约束哪个文件?targetNamespace后面就是被约束文件的大名。通缉令上写上大名:抓捕汉奸王二麻子,然后捕头就按图索骥,寻找王二麻子这个人。下面是王二麻子:

代码002

1
<?xml version="1.0"?> 2 <note xmlns="http://www.w3school.com.cn" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.w3school.com.cn
             http://www.w3school.com.cn /note.xsd"
> 5 <to>George</to> 6 <from>John</from> 7 <heading>Reminder</heading> 8 <body>Don't forget the meeting!</body> 9 </note>

看到木有,第2行xmlns后面,行不改名,坐不改姓,http://www.w3school.com.cn这就是这个配置文件内定义的大名(代号王二麻子)。5到8行是这个bean配置文件的属性,这些属性遵循的规则就是前面讲到的代码001的5-14行约束。

这一行的xmlns后面本来也有个冒号+小名的,不写表示默认命名空间,spring中bean的定义都使用默认命名空间。

 

那么这个xmlns:xsi又是什么呢?兄弟你记住一点,xmlns就是"法",它后面的东西都是定义的规则的大名和小名。

你看,代码001中第三行说明,规则的小名叫xs,规则的大名叫http://www.w3.org/2001/XMLSchema。

那么,代码002中,xsi就是规则小名,后面的http://www.w3.org/2001/XMLSchema-instance就是规则的大名了。(跟代码001不同的是,001是自定义的,而xsi是spring原配的。)

第4行是调用xsi规则的schemaLocation子规则,这个规则是寻找具体约束它的文件。(通过后面的链接就可以找到代码001这个文件。)

 

与xmlns:xsi类似,spring内置了很多Schema约束文件,如:beans、aop、tx、mvc、util等等,不一而足。

 

有了上面的栗子,spring项目里具体的xml文件就有所了解了,再来看,下面是一个小栗子的配置文件:

代码003

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" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xsi:schemaLocation="http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context-3.0.xsd 12 http://www.springframework.org/schema/tx 13 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 14 http://www.springframework.org/schema/aop 15 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 16 17 <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 --> 18 <context:component-scan base-package="com.baobaotao.dao"/> 19 <context:component-scan base-package="com.baobaotao.service"/> 20 21 <!-- 配置数据源 --> 22 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 23 destroy-method="close" 24 p:driverClassName="com.mysql.jdbc.Driver" 25 p:url="jdbc:mysql://localhost:3306/sampledb" 26 p:username="root" 27 p:password="2009118293zjl" /> 28 29 <!-- 配置Jdbc模板 --> 30 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" 31 p:dataSource-ref="dataSource" /> 32 33 <!-- 配置事务管理器 --> 34 <bean id="transactionManager" 35 class="org.springframework.jdbc.datasource.DataSourceTransactionManager" 36 p:dataSource-ref="dataSource" /> 37 38 <!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 --> 39 <aop:config proxy-target-class="true"> 40 <aop:pointcut id="serviceMethod" 41 expression=" execution(* com.baobaotao.service..*(..))" /> 42 <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" /> 43 </aop:config> 44 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 45 <tx:attributes> 46 <tx:method name="*" /> 47 </tx:attributes> 48 </tx:advice> 49 </beans>

代码003中,3-7行都是spring的内置xsd,8-15行是调用xsi的schemaLocation去寻找具体的xsd位置。18-48行都是为各个规则定义了一些约束对象,王二麻子一箩筐。

如果你的电脑连了网,在myeclipse中打开这个xml的时候,你可以随便挑一个规则,鼠标指在后面的链接上,按住ctrl键,点击链接,比如点击http://www.springframework.org/schema/tx/spring-tx-3.0.xsd这个链接,它会直接从内置浏览器通过访问链接打开这个xsd文件,下面是打开后的部分代码:

 1 <?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
 2 - <xsd:schema xmlns="http://www.springframework.org/schema/tx" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:tool="http://www.springframework.org/schema/tool" targetNamespace="http://www.springframework.org/schema/tx" elementFormDefault="qualified" attributeFormDefault="unqualified">
 3   <xsd:import namespace="http://www.springframework.org/schema/beans" schemaLocation="http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" /> 
 4   <xsd:import namespace="http://www.springframework.org/schema/tool" schemaLocation="http://www.springframework.org/schema/tool/spring-tool-3.0.xsd" /> 
 5 - <xsd:annotation>
 6 - <xsd:documentation>
 7 - <![CDATA[ 
 8     Defines the elements used in the Spring Framework's declarative
 9     transaction management infrastructure.
10         
11 
12   ]]> 
13   </xsd:documentation>
14   </xsd:annotation>
15 - <xsd:element name="advice">

这个就是tx约束文件,读者可以大概看一下前面几行的意思,这里不做详解。

但是有个问题,一般情况下开发项目怎么可能保证联网呢?不联网的情况下这些约束如何生效?

其实,spring的jar包中都放了这些xsd文件。比如用压缩工具打开org.springframework.beans-3.0.5.RELEASE.jar这个jar包,

 

这里面都有。

 

Spring除了基于XML的配置,还有基于Bean,基于注解的配置。但是基于XML的配置功能最强,是基础,是必须首先要了解的。

 

 

天道酬勤。共勉。

 

 

  

  

posted on 2015-11-09 01:27  搬砖的小学生  阅读(2723)  评论(1编辑  收藏  举报