Spring使用p名稱空間配置屬性


給XML配置文件"減肥"的另一個選擇就是使用p名稱空間,從 2.0開始,Spring支持使用名稱空間的可擴展配置格式。這些名稱空間都是基於一種XML Schema定義。事實上,我們所看到的所有bean的配置格式都是基於一個 XML Schema文檔。

特定的名稱空間並不需要定義在一個XSD文件中,它只在Spring內核中存在。我們所說的p名稱空間就是這樣,它不需要一個schema定義,與我們前面采用<property/>元素定義bean的屬性不同的是,當我們采用了p名稱空間,我們就可以在bean元素中使用屬性(attribute)來描述bean的property值。

下面的兩段XML配置文件中都是用來定義同一個bean:一個采用的是標准的XML格式,一個是采用p名稱空間。

<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
    <bean name="classic" class="com.example.ExampleBean">
        <property name="email" value="foo@bar.com/>
    </bean>
    
    <bean name="p-namespace" class="com.example.ExampleBean"
          p:email="foo@bar.com"/>
</beans>

從上面的bean定義中,我們采用p名稱空間的方式包含了一個叫email的屬性,而Spring會知道我們的bean包含了一個屬性(property)定義。我們前面說了,p名稱空間是不需要schema定義的,因此屬性(attribute)的名字就是你bean的property的名字。

This next example includes two more bean definitions that both have a reference to another bean:

下面的例子包含了兩個bean定義,它們都引用了另一個bean

<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
    <bean name="john-classic" class="com.example.Person">
        <property name="name" value="John Doe"/>
        <property name="spouse" ref="jane"/>
    </bean>

    <bean name="john-modern" 
        class="com.example.Person"
        p:name="John Doe"
        p:spouse-ref="jane"/>

    <bean name="jane" class="com.example.Person">
        <property name="name" value="Jane Doe"/>
    </bean>
</beans>

As you can see, this example doesn't only include a property value using the p-namespace, but also uses a special format to declare property references. Whereas the first bean definition uses <property name="spouse" ref="jane"/> to create a reference from bean john to bean jane, the second bean definition uses p:spouse-ref="jane" as an attribute to do the exact same thing. In this case 'spouse' is the property name whereas the '-ref' part indicates that this is not a straight value but rather a reference to another bean.

上面的例子不僅使用p名稱空間包含了一個屬性(property)值,而且使用了一個特殊的格式聲明了一個屬性引用。在第一個bean定義中使用了<property name="spouse" ref="jane"/>來建立beanjohn到beanjane的引用,而第二個bean定義則采用p:spouse-ref="jane"屬性(attribute)的方式達到了同樣的目的。在這個例子中,"spouse"是屬性(property)名,而"-ref“則用來說明該屬性不是一個具體的值而是對另外一個bean的引用。

注意

需要注意的是,p名稱空間沒有標准的XML格式定義靈活,比如說,bean的屬性名是以Ref結尾的,那么采用p名稱空間定義就會導致沖突,而采用標准的XML格式定義則不會出現這種問題。這里我們提醒大家在項目中還是仔細權衡來決定到底采用那種方式,同時也可以在團隊成員都理解不同的定義方式的基礎上,在項目中根據需要同時選擇三種定義方式。

 

轉:http://blog.csdn.net/liaomin416100569/article/details/4924899


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM