Spring配置文件的約束信息深入理解


????要問當(dāng)下Java的什么技術(shù)在實(shí)際生產(chǎn)開發(fā)中最流行,那當(dāng)然是Spring全家桶,Spring為實(shí)際開發(fā)提供了豐富的技術(shù)支持,本篇文章從Spring基礎(chǔ)出發(fā),理解Spring配置的約束信息的含義,以避免大家在實(shí)際開發(fā)中為尋找Spring配置的約束信息而苦惱。

一、常用的Spring配置約束

<?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: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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <!--配置文件部分-->
    <bean id="txManager"    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="find*" propagation="NOT_SUPPORTED" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:aspect id="***" ref="***"/>
        <aop:pointcut id="***" expression="****" />
    </aop:config>
    <!--中間是配置文件部分-->
    </beans>

二、命名空間

????命名空間是由國(guó)際化資源標(biāo)識(shí)符 (IRI) 標(biāo)識(shí)的 XML 元素和屬性集合,簡(jiǎn)單點(diǎn)說,就是為你的核心配置提供標(biāo)簽使用的限定范圍的一個(gè)約束信息。每個(gè)配置文件最多有一個(gè)默認(rèn)的命名空間,當(dāng)沒有默認(rèn)命名空間時(shí),就得手動(dòng)配置標(biāo)簽頭尋找標(biāo)簽,即不帶前綴的xmlns約束

xmlns="http://www.springframework.org/schema/beans"

但是例如帶前綴的約束,如

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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、context、aop、tx代表的對(duì)應(yīng)命名空間的元素,意思就是你的配置文件的標(biāo)簽元素必須來自于你配置的約束范圍,否則無法找到標(biāo)簽。例如上面代碼中的如下部分使用的標(biāo)簽<bean/>就是來自默認(rèn)命名空間、<tx:advice/>標(biāo)簽就是來自 xmlns:tx="http://www.springframework.org/schema/tx"命名空間,類似地,其他的標(biāo)簽也是來自相應(yīng)的命名空間。

 <bean id="txManager"    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="find*" propagation="NOT_SUPPORTED" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:aspect id="***" ref="***"/>
        <aop:pointcut id="***" expression="****" />
    </aop:config>

那么,為什么會(huì)有schemaLocation的配置呢?顧名思義,找到標(biāo)簽命名空間對(duì)應(yīng)的Location,也就是地址唄,配置成你需要使用的版本地址即可。另外,例如你要使用ref注入你自己配置的bean的name的時(shí)候,如果沒有默認(rèn)的命名空間,你必須自己手動(dòng)加上bean命名空間的前綴。

三、Spring配置文件的命名空間

????spring 整合了各種工具,并且spring提供了對(duì)各種工具的xml scheme 的配置方式,簡(jiǎn)化了開發(fā)。對(duì)于各種工具的xml命名空間的引入,我們也應(yīng)該有一個(gè)比較清楚的認(rèn)識(shí)。Spring在啟動(dòng)時(shí)是要檢驗(yàn)XML文件的。如果xml空間存在命名空間內(nèi)沒有的元素是要報(bào)錯(cuò)的。通常情況下,命名空間對(duì)應(yīng)的URI是一個(gè)存放XSD的地址,盡管規(guī)范沒有這么要求。如果沒有提供schemaLocation,那么Spring的XML解析器會(huì)從命名空間的URI里加載XSD文件。

四、Spring如何去尋找XML對(duì)應(yīng)的地址

????Spring默認(rèn)在啟動(dòng)時(shí)是要從配置的命名空間的位置加載XSD文件來驗(yàn)證xml文件的,所以如果有的時(shí)候斷網(wǎng)了,或者一些開源軟件切換域名,那么就很容易碰到應(yīng)用啟動(dòng)不了。為了防止這種情況,Spring提供了一種機(jī)制,即默認(rèn)從本地加載XSD文件,當(dāng)本地沒有時(shí)才根據(jù)實(shí)際的URI去聯(lián)網(wǎng)獲得。我們打開Spring-aop-4.1.6RELEASE.jar (這是我本地的版本),這個(gè)包下有一個(gè)META_INF文件夾,其中有兩個(gè)文件:spring.handlers和spring.schemas。
spring.handlers:

http://www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler

spring.schemas:

http://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd=org/springframework/aop/config/spring-aop-3.1.xsd
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd=org/springframework/aop/config/spring-aop-3.2.xsd
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd=org/springframework/aop/config/spring-aop-4.0.xsd
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd=org/springframework/aop/config/spring-aop-4.1.xsd
http://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-4.1.xsd

Spring是把XSD文件放到本地了,再在spring.schemas里做了一個(gè)映射,優(yōu)先從本地里加載XSD文件。所以系統(tǒng)可以離線情況下從本地加載相關(guān)的xsd信息。

五、總結(jié)

1.Spring配置文件的約束信息可根據(jù)自己的需求進(jìn)行配置,再也不用糾結(jié)哪些命名空間需要哪些不需要。
2.企業(yè)級(jí)開發(fā)中,大部分都是用自己封裝的標(biāo)簽,原理其實(shí)相同,配置方式完全一致。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容