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

时间:2023-12-24 10:12:37

要深入了解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"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com.cn">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

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

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

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

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

代码002

1 <?xml version="1.0"?>
<note xmlns="http://www.w3school.com.cn"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3school.com.cn
             http://www.w3school.com.cn /note.xsd">
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</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" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
<context:component-scan base-package="com.baobaotao.dao"/>
<context:component-scan base-package="com.baobaotao.service"/> <!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/sampledb"
p:username="root"
p:password="2009118293zjl" /> <!-- 配置Jdbc模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
p:dataSource-ref="dataSource" /> <!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" /> <!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceMethod"
expression=" execution(* com.baobaotao.service..*(..))" />
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
</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文件,下面是打开后的部分代码:

 <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
- <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">
<xsd:import namespace="http://www.springframework.org/schema/beans" schemaLocation="http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" />
<xsd:import namespace="http://www.springframework.org/schema/tool" schemaLocation="http://www.springframework.org/schema/tool/spring-tool-3.0.xsd" />
- <xsd:annotation>
- <xsd:documentation>
- <![CDATA[
Defines the elements used in the Spring Framework's declarative
transaction management infrastructure. ]]>
</xsd:documentation>
</xsd:annotation>
- <xsd:element name="advice">

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

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

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

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

这里面都有。

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

天道酬勤。共勉。