Spring为字面值、引用Bean和集合都提供了简化的配置方式,如果没有用到完整格式的特殊功能,大可使用简化的配置方式。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="plane" class="com.xgj.ioc.inject.set.Plane">
<property name="brand" value="Airbus&A380"/>
<property name="color" value="red"/>
<property name="speed" value="700">property>
bean>
beans>
使用简化方式,则无法使用
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="tank" class="com.xgj.ioc.inject.construct.type.Tank">
<constructor-arg type="java.lang.String" value="T72"/>
<constructor-arg type="double" value="20000.00"/>
bean>
beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="pets" class="com.xgj.ioc.inject.construct.jihe.strongType.Pets">
<property name="map">
<map>
<entry key="111" value="cat"/>
<entry key="113" value="bird"/>
<entry key="115" value="dog"/>
map>
property>
bean>
<bean id="petShop" class="com.xgj.ioc.inject.construct.jihe.strongType.PetShop">
<property name="pets" ref="pets" />
bean>
beans>
复杂的方式
<property name="plane">
<ref bean="plane"/>
property>
简化的方式
<property name="plane" ref="plane"/>
复杂的方式
<constructor-arg>
<ref bean="plane">
constructor-arg>
简化的方式
<constructor-arg ref="plane">
复杂的方式
<map>
<entry>
<key>
<ref bean="keyBean"/>
key>
<ref bean="valueBean"/>
entry>
map>
简化的方式
<map>
<entry key-ref="keyBena" value-ref="valueBean"/>
</map>
ref标签的简化形式对应于 而 和 没有对应的简化形式
Spring2.5版本之后引入了新的p命名空间,可以通过bean元素属性的方式配置bean的属性。
使用p命名空间后,基于XML的配置方式将进一步简化。
需要引入
xmlns:p="http://www.springframework.org/schema/p"
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
引入p命名空间
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="plane" class="com.xgj.ioc.inject.set.Plane"
p:brand="Airbus&A380"
p:color="red"
p:speed="700"
>
bean>
beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="police" class="com.xgj.ioc.inject.construct.ref.Police"
p:gun-ref="gun"/>
<bean id="gun" class="com.xgj.ioc.inject.construct.ref.Gun" />
beans>
未采用p命名空间前,使用子元素配置Bean的属性, 采用p命名空间后,采用的元素属性配置Bean的属性。
对于字面值属性,其格式为
p:<属性名>-ref="xxx"
对已引用对象的属性,其格式为:
p:<属性名>-ref="xxx"
正是由于p命名空间中的属性名是可变的,所以p命名空间没有对应的schema定义文件,也无需在xsi:schemaLocation中为p命名空间指定Schema定义文件.