【Spring学习笔记-2】IOC Bean的配置方式

目录

bean的创建顺序

bean 对象的单例和多例

通过属性配置Bean

通过构造器配置Bean

通过 p 名称空间配置 Bean

 通过 ref 注入其他Bean

配置内部Bean

引用/注入集合/数组类型

级联属性赋值

bean 配置信息重用(继承)


bean的创建顺序

在 spring 的 ioc 容器, 默认是按照xml文件里配置的从上到下的顺序创建 bean 对象


bean 对象的单例和多例

在 spring 的 ioc 容器,在默认是按照单例创建的,即配置一个 bean 对象后,ioc 容器只会创建一个 bean 实例。

如果,我们希望 ioc 容器配置的某个 bean 对象,是以多个实例形式创建的则可以通过配置scope="prototype" 来指定。

<bean name="student04" scope="prototype" class="com.asl.spring.beans.Student"/>

通过属性配置Bean

    <bean class="com.asl.spring.Student" id="student01">
        <property name="name" value="jack01"/>
        <property name="sex" value="man"/>
        <property name="age" value="18"/>
    </bean>

通过构造器配置Bean

    <bean class="com.asl.spring.Student" id="student03">
        <constructor-arg value="jack03" index="0" type="java.lang.String"/>
        <constructor-arg value="man" index="1" type="java.lang.String"/>
        <constructor-arg value="18" index="2" type="java.lang.String"/>
    </bean>

通过 p 名称空间配置 Bean

    <bean class="com.asl.spring.Student" id="student04"
          p:name="jack04"
          p:sex="man"
          p:age="14"
    />

 通过 ref 注入其他Bean

    <bean class="类路径" id="id01"/>

    <bean class="类路径" id="id02">
        <property name="属性名" ref="id01"/>
    </bean>

配置内部Bean


    <bean class="" id=""/>
    <bean class="" id="">
        <property name="">
            <bean class=""/>
        </property>
    </bean>

引用/注入集合/数组类型

        <!--配置list属性-->
        <property name="">
            <list>
                <ref bean=""/>
                <ref bean=""/>
                <ref bean=""/>
            </list>
        </property>

        <!--配置map属性-->
        <property name="">
            <map>
                <entry key="" value-ref=""/>
                <entry key="" value-ref=""/>
            </map>
        </property>
        <!--配置set属性-->
        <property name="">
            <set>
                <ref bean=""/>
                <ref bean=""/>
                <ref bean=""/>
            </set>
        </property>
        <!--配置数组-->
        <property name="">
            <array>
                <value>value1</value>
                <value>value2</value>
                <value>value3</value>
                <value>value4</value>
            </array>
        </property>
        <!--配置properties-->
        <property name="properties">
            <props>
                <prop key="username">root</prop>
                <prop key="password">123456</prop>
            </props>
        </property>

级联属性赋值

    <bean class="" id="a01"/>
    <bean class="" id="a02">
        <property name="a01" ref="a01"/>
        <property name="a01.name" value=""/>
    </bean>

bean 配置信息重用(继承)

    <!--如果配置了abstract属性,那么就不能被实例化,只能被用于继承-->
    <!-- 当我们把某个bean设置为 abstract="true" 这个bean只能被继承,而不能实例化了 -->
    <bean class="com.asl.spring.beans.Student" id="student11" >
        <property name="name" value="jack"/>
        <property name="sex" value="man"/>
        <property name="age" value="18"/>
    </bean>
    <!-- parent="student12" 就是继承使用了 student11 的配置信息 -->
    <bean class="com.asl.spring.beans.Student" id="student12" parent="student11"/>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值