Spring-注入参数详解-[简化配置方式]

概述

Spring为字面值、引用Bean和集合都提供了简化的配置方式,如果没有用到完整格式的特殊功能,大可使用简化的配置方式。

字面属性值

字面值属性

<?xml version="1.0" encoding="UTF-8"?>
<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&amp;A380</value>
        </property>

        <property name="color">
            <value>red</value>
        </property>

        <property name="speed">
            <value>700</value>
        </property>
    --> 
    <!-- 简化配置方式 -->
    <property name="brand" value="Airbus&amp;A380"/>
    <property name="color" value="red"/>
    <property name="speed" value="700"></property>
    </bean>
</beans>

使用简化方式,则无法使用

构造函数参数

<?xml version="1.0" encoding="UTF-8"?>
<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</value>
        </constructor-arg>

        <constructor-arg type="double">
            <value>20000.00</value>
        </constructor-arg>
         -->

         <!-- 简化方式 -->
         <constructor-arg type="java.lang.String"  value="T72"/>
         <constructor-arg  type="double" value="20000.00"/>

    </bean>
</beans>

集合元素

<?xml version="1.0" encoding="UTF-8"?>
<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>
                        为Integer提供值,spring在设置值时,会转换为定义的Integer类型 
                        <value>111</value>
                    </key>
                    <value>cat</value>
                </entry>
                <entry>
                    <key>
                        <value>113</value>
                    </key>
                    <value>bird</value>
                </entry>
                <entry>
                    <key>
                        <value>115</value>
                    </key>
                    <value>dog</value>
                </entry>
            </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标签的简化形式对应于<ref bean="xxx"><ref local="xxx"><ref parent="xxx"> 没有对应的简化形式


使用P命名空间

Spring2.5版本之后引入了新的p命名空间,可以通过bean元素属性的方式配置bean的属性。

使用p命名空间后,基于XML的配置方式将进一步简化。

需要引入

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

通过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"
    引入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">


    <!-- 使用p命名空间 -->
    <bean id="plane" class="com.xgj.ioc.inject.set.Plane"
        p:brand="Airbus&amp;A380"
        p:color="red"
        p:speed="700"
    >
    </bean>

</beans>

通过p命名空间引用其他bean

<?xml version="1.0" encoding="UTF-8"?>
<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">
        <property name="gun">
             通过ref应用容器中的gun ,建立police和gun的依赖关系 
            <ref bean="gun" />
        </property>
    </bean>
     -->

     <!-- 使用p命名空间  引用其他bean-->
    <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>使用<property>子元素配置Bean的属性,
采用p命名空间后,采用<bean>的元素属性配置Bean的属性。

对于字面值属性,其格式为

p:<属性名>-ref="xxx"

对已引用对象的属性,其格式为:

p:<属性名>-ref="xxx"

正是由于p命名空间中的属性名是可变的,所以p命名空间没有对应的schema定义文件,也无需在xsi:schemaLocation中为p命名空间指定Schema定义文件.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小工匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值