Spring中的p命名空間和c命名空間

p命名空間和c命名空間


本文轉自:https://blog.csdn.net/elim168/article/details/74516439

在通過構造方法或set方法給bean注入關聯項時通常是通過constructor-arg元素和property元素來定義的。在有了p命名空間和c命名空間時我們可以簡單的把它們當做bean的一個屬性來進行定義。

p命名空間

使用p命名空間時需要先聲明使用對應的命名空間,即在beans元素上加入xmlns:p=“http://www.springframework.org/schema/p”。下面先來看一個示例。

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="world" class="com.app.World"/>
	
	<!-- 通過set方法注入的傳統的bean定義 -->
	<bean id="hello1" class="com.app.Hello">
		<property name="p1" value="v1"/>
		<property name="p2" value="v2"/>
		<property name="world" ref="world"/>
	</bean>
	
	<!-- 通過set方法注入的使用p命名空間的bean定義 -->
	<bean id="hello2" class="com.app.Hello" p:p1="v1" p:p2="v2" p:world-ref="world"/>

</beans>

在上面示例中,id爲hello1的bean是傳統的bean定義,而id爲hello2的bean是基於p命名空間的bean定義。當傳統的property元素定義的value是基礎數據類型時,我們可以直接把property元素對應的name加上p命名空間的前綴作爲bean的一個屬性進行定義,對應的值就是原property元素對應的value。如上述示例中name爲“p1”的property使用p命名空間後就變成了“p:p1”;當傳統的property元素定義的是對其它bean的關聯時,我們可以直接把property元素對應的name加上“-ref”,再加上p命名空間的前綴作爲bean的一個屬性進行定義,對應的值爲原property元素對應的ref值,如上述示例中name爲“world”的property就是定義了對其它bean的關聯,使用p命名空間後就變成了“p:world-ref”。這裏有一點需要注意的地方就是property對應的是set方法,而不是對應的屬性,如name爲“world”的property實際上對應的是setWorld()方法,這個時候不管對應的bean是否真存在名爲world的屬性;另一點需要注意的地方是使用p命名空間時要注意以“-ref”結尾的property,這會導致Spring以其前部分作爲property,因爲“-ref”會被Spring作爲關聯的關鍵字。

c命名空間

c命名空間的用法和p命名空間類似,其對應於constructor-arg,即可以將constructor-arg元素替換爲bean的一個以c命名空間前綴開始的屬性。使用c命名空間之前也需要通過xmlns:c=”http://www.springframework.org/schema/c”進行聲明。

<?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:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="world" class="com.app.World"/>
	
	<!-- 傳統的使用constructor-arg通過構造方法注入的bean定義 -->
	<bean id="hello1" class="com.app.Hello">
		<constructor-arg index="0" value="arg1"/>
		<constructor-arg index="1" value="2"/><!-- arg2 -->
		<constructor-arg index="2" ref="world"/><!-- arg3 -->
	</bean>
	<!-- 使用c命名空間通過構造方法注入的bean定義 -->
	<bean id="hello2" class="com.app.Hello" c:arg1="c_arg1" c:arg2="2" c:arg3-ref="world"/>
</beans>

如上所示,c命名空間的用法和p命名空間的用法類似。對於通過構造方法注入原始類型的對象可以把對應的構造參數名稱加上c命名空間的前綴作爲bean的一個屬性進行定義,對應的值即是構造參數的值;如果通過構造參數注入的是其它bean的一個引用,則可將該構造參數名稱加上“-ref”,再加上c命名空間的前綴作爲該bean的一個屬性進行定義,對應的值爲所關聯bean的id或name,如上述示例中的“c:arg3-ref”。

需要注意的是直接把構造參數名稱加上c命名空間的前綴作爲bean的一個屬性定義來替代對應的constructor-arg只對以debug方式編譯的class有效,因爲對於非debug方式編譯的class文件Spring將無法獲取到對應構造方法的參數名。對於這種情況我們可以直接使用構造方法參數的索引加上下劃線“_”前綴來代替對應的參數名,索引是從0開始的,如上面的示例以索引來代替時將是如下這個樣子。

<?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:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="world" class="com.app.World"/>
	
	<!-- 傳統的使用constructor-arg通過構造方法注入的bean定義 -->
	<bean id="hello1" class="com.app.Hello">
		<constructor-arg index="0" value="arg1"/>
		<constructor-arg index="1" value="2"/><!-- arg2 -->
		<constructor-arg index="2" ref="world"/><!-- arg3 -->
	</bean>
	<!-- 使用c命名空間並且是使用構造參數的索引作爲屬性來通過構造方法注入的bean定義 -->
	<bean id="hello2" class="com.app.Hello" c:_0="c_arg1" c:_1="2" c:_2-ref="world"/>
</beans>

(注:本文是基於Spring4.1.0所寫)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章