深入解读Spring Framework IoC容器(第三弹:依赖注入配置详解)

本篇我们介绍下常用的依赖注入配置。

直接变量

<property/>元素的value值可以通过字符串形式来指定属性和构造器参数。

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
    <property name="username" value="root"/>
    <property name="password" value="masterkaoli"/>
</bean>

idref元素

idref元素用来传递容器内其他bean的id值,例如:

<bean id="theTargetBean" class="..."/>

<bean id="theClientBean" class="...">
    <property name="targetName">
        <idref bean="theTargetBean" />
    </property>
</bean>

这段配置就相当于

<bean id="theTargetBean" class="..." />

<bean id="theClientBean" class="...">
    <property name="targetName" value="theTargetBean" />
</bean>

区别在于使用idref标签容器会在部署时验证引用的bean是否存在。这个标签可以在我们要使用其他bean的id值的时候防止我们拼错。

ref元素

ref元素用来将bean中指定属性的值设置为对容器的另外一个bean的引用。 该引用bean将被作为依赖注入。例如:
<ref bean="someBean"/>

内部bean

内部bean(inner bean)是指在<property/>或者<constructor-arg/>元素内部使用<bean/>定义bean。内部bean没有id、name属性以及scope。内部bean总是匿名的并且他们总是伴随着外部bean创建。内部bean也不能被注入到其他的bean。

<bean id="outer" class="...">
    <property name="target">
        <!-- 这就是内部bean -->
        <bean class="com.example.Person"> 
            <property name="name" value="Fiona Apple"/>
            <property name="age" value="25"/>
        </bean>
    </property>
</bean>

集合

集合元素有<list/><set/><map/>,和<props/>,写法如下:

<bean id="moreComplexObject" class="example.ComplexObject">
    <!-- props对应java.util.Properties -->
    <property name="adminEmails">
        <props>
            <prop key="administrator">administrator@example.org</prop>
            <prop key="support">support@example.org</prop>
            <prop key="development">development@example.org</prop>
        </props>
    </property>
    <!-- list对应java.util.List -->
    <property name="someList">
        <list>
            <value>a list element followed by a reference</value>
            <ref bean="myDataSource" />
        </list>
    </property>
    <!-- map对应java.util.Map -->
    <property name="someMap">
        <map>
            <entry key="an entry" value="just some string"/>
            <entry key ="a ref" value-ref="myDataSource"/>
        </map>
    </property>
    <!-- set对应java.util.Set -->
    <property name="someSet">
        <set>
            <value>just some string</value>
            <ref bean="myDataSource" />
        </set>
    </property>
</bean>

集合合并

<beans>
    <bean id="parent" abstract="true" class="example.ComplexObject">
        <property name="adminEmails">
            <props>
                <prop key="administrator">administrator@example.com</prop>
                <prop key="support">support@example.com</prop>
            </props>
        </property>
    </bean>
    <bean id="child" parent="parent">
        <property name="adminEmails">
            <!-- 两种不同类型的集合是不能合并的 -->
            <!-- 当子bean被容器初始化,其adminEmails将与父bean的adminEmails属性进行合并。 -->
            <!-- 子bean的集合从父bean集成所有属性元素,同时子bean的相同key的prop值将覆盖父集合的相应值。 -->
            <!-- merge属性必须在继承的子bean中定义 -->
            <props merge="true">
                <prop key="sales">sales@example.com</prop>
                <prop key="support">support@example.co.uk</prop>
            </props>
        </property>
    </bean>
<beans>

对于list要注意,父bean的list内容将排在子bean的list内容的前面。

强类型集合转换

public class Foo {

    private Map<String, Float> accounts;

    public void setAccounts(Map<String, Float> accounts) {
        this.accounts = accounts;
    }
}
<beans>
    <bean id="foo" class="x.y.Foo">
        <!-- 这个属性准备注入时,通过反射获得代码中定义强类型Map<String, Float>,可以自动转换 -->
        <property name="accounts">
            <map>
                <entry key="one" value="9.99"/>
                <entry key="two" value="2.75"/>
                <entry key="six" value="3.99"/>
            </map>
        </property>
    </bean>
</beans>

Null和空字符串

<bean class="ExampleBean">
    <property name="email" value=""/>
</bean>
<bean class="ExampleBean">
    <property name="email">
        <null/>
    </property>
</bean>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值