7.依赖注入之为list,map集合类型的属性赋值以及p命名空间

一些其他ioc管理的bean属性 

<bean id="studentOne" class="com.tian.spring.pojo.Student"></bean>

    <bean id="studentTwo" class="com.tian.spring.pojo.Student">
        <property name="sid" value="1001"></property>
        <property name="sname" value="张三"></property>
        <property name="age" value="23"></property>
        <property name="gender" value="男"></property>
    </bean>
    <bean id="studentThree" class="com.tian.spring.pojo.Student">
        <constructor-arg value="1002"></constructor-arg>
        <constructor-arg value="李四"></constructor-arg>
        <constructor-arg value="女"></constructor-arg>
        <constructor-arg value="80" name="age"></constructor-arg>
    </bean>

    <bean id="studentFour" class="com.tian.spring.pojo.Student">
        <property name="sid" value="1003"></property>
        <!--
            <:&lt;
            >:&gt;
        -->
<!--        <property name="sname" value="&lt;王五&gt;"></property>-->
        <property name="sname">
            <value><![CDATA[<王五>]]></value>
        </property>
        <property name="gender">
            <null/>
        </property>
    </bean>

    <bean id="studentFive" class="com.tian.spring.pojo.Student">
        <property name="sid" value="1004"></property>
        <property name="sname" value="赵六"></property>
        <property name="age" value="26"></property>
        <property name="gender" value="男"></property>
        <property name="clazz">
            <!--内部bean,只能在当前bean的内部使用,不能直接通过IOC容器获取-->
            <bean id="clazzInner" class="com.tian.spring.pojo.Clazz">
                <property name="cid" value="2222"></property>
                <property name="cname" value="远大前程班"></property>
            </bean>
        </property>

        <property name="hobby">
            <array>
                <value>唱</value>
                <value>跳</value>
                <value>rap</value>
            </array>
        </property>

一:List集合

1:通过List标签来进行赋值

    <bean id="clazzOne" class="com.tian.spring.pojo.Clazz">
        <property name="cid" value="1111"></property>
        <property name="cname" value="最强王者班"></property>
        <property name="students">
            <list>
                <ref bean="studentOne"></ref>
                <ref bean="studentTwo"></ref>
                <ref bean="studentThree"></ref>
            </list>
        </property>
    </bean>

2:通过配置一个List集合类型的bean,但是需要用到util的一个约束

    <!--配置一个集合类型的bean,需要使用util的约束-->
    <util:list id="studentList">
        <ref bean="studentOne"></ref>
        <ref bean="studentTwo"></ref>
        <ref bean="studentThree"></ref>
    </util:list>
    <bean id="clazzOne" class="com.tian.spring.pojo.Clazz">
        <property name="cid" value="1111"></property>
        <property name="cname" value="最强王者班"></property>
        <property name="students" ref="studentList"></property>
    </bean>

二:Map集合

1:通过map标签来进行赋值

    <bean id="teacherOne" class="com.tian.spring.pojo.Teacher">
        <property name="tid" value="10086"></property>
        <property name="tname" value="大宝"></property>
    </bean>
    <bean id="teacherTwo" class="com.tian.spring.pojo.Teacher">
        <property name="tid" value="10010"></property>
        <property name="tname" value="小宝"></property>
    </bean>
        <property name="teacherMap">
            <map>
                <entry key="10086" value-ref="teacherOne"></entry>
                <entry key="10010" value-ref="teacherTwo"></entry>
            </map>
        </property>

2:通过配置一个Map集合类型的bean,但是需要用到util的一个约束

    <util:map id="teacherMap">
        <entry key="10086" value-ref="teacherOne"></entry>
        <entry key="10010" value-ref="teacherTwo"></entry>
    </util:map>
 <property name="teacherMap" ref="teacherMap"></property>

三:p命名空间    

    <bean id = "studentSix" class="com.tian.spring.pojo.Student"
          p:sid="1005" p:sname="小明" p:teacherMap-ref="teacherMap">
    </bean>

没带ref的是给字面量类型赋值的,带ref是给类类型属性去引用某一个bean的id的 

    @Test
    public void testDI(){
        //获取IOC容器
        ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
        //获取bean
        Student student = ioc.getBean("studentSix", Student.class);
        System.out.println(student);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值