p命名空間
使用p命名空間可以用bean 元素的屬性代替
<property/>
元素。
<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">
<!-- 标準XML格式 -->
<bean name="john-classic" class="com.example.Person">
<property name="name" value="John Doe"/>
<property name="spouse" ref="jane"/>
</bean>
<!-- p命名空間格式 -->
<bean name="john-modern"
class="com.example.Person"
p:name="John Doe"
p:spouse-ref="jane"/>
</beans>
c命名空間
使用c命名空間可以用内聯的構造參數代替嵌套的constructor-arg元素。
<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="bar" class="x.y.Bar"/>
<bean id="baz" class="x.y.Baz"/>
<!-- 标準XML格式 -->
<bean id="foo" class="x.y.Foo">
<constructor-arg name="bar" ref="bar"/>
<constructor-arg name="baz" ref="baz"/>
<constructor-arg name="email" value="[email protected]"/>
</bean>
<!-- c命名空間格式 -->
<bean id="foo" class="x.y.Foo" c:bar-ref="bar" c:baz-ref="baz" c:email="[email protected]"/>
<!-- 還可以使用c命名空間的參數索引格式 -->
<bean id="foo" class="x.y.Foo" c:_0-ref="bar" c:_1-ref="baz" c:_2="[email protected]"/>
</beans>
雖然p命名空間和c命名空間都可以簡化XML配置,但是在實際開發中還是建議使用标準XML格式。