要深入了解Spring機制,首先需要知道Spring是怎樣在IoC容器中裝配Bean的。而了解這一點的前提是,要搞清楚Spring基於Schema的Xml配置方案。
在深入了解之前,必須要先明白幾個標簽的意思(我會逐步引導讀者理解,剛開始的懵懂無所謂,讀者自會漸入佳境。初極狹,才通人。復行數十步,豁然開朗。)。
- 什么是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的配置功能最強,是基礎,是必須首先要了解的。
天道酬勤。共勉。
