首页 > 编程语言 > 详细

SpringIOC容器-xml配置bean-反射构造器-属性注入(集合类型)

时间:2020-04-17 17:09:01      阅读:100      评论:0      收藏:0      [点我收藏+]

一、实验使用实体类

技术分享图片

技术分享图片

RichMan类中有一个属性的类型是List<Car>类型

GreatMan类中有一个属性的类型是Map<String,Car>类型

这里不再使用Car类的有参构造方法,因为Car类这里只是为了new对象被RichMan和GreatMan所注入

1.将List注入,如果是引用类型,采用ref标签,如果是字面量(基本数据类型或String类型)采用value标签。

  这里的List只能在bean中使用,不能被其他bean使用

<bean id="car1" class="com.llf.bean.Car">
    <property name="brand" value="宝马"></property>
    <property name="price" value="1000000.00"></property>
    <property name="speed" value="300"></property>
</bean>
<bean id="car2" class="com.llf.bean.Car">
    <property name="brand" value="奔驰"></property>
    <property name="price" value="1500000.00"></property>
    <property name="speed" value="320"></property>
</bean>
<bean id="car3" class="com.llf.bean.Car">
    <property name="brand" value="宾利"></property>
    <property name="price" value="18000000"></property>
    <property name="speed" value="400"></property>
</bean>

<bean id="richMan" class="com.llf.bean.RichMan">
    <property name="name" value="马云"></property>
    <property name="age" value="55"></property>
    <property name="cars" >
        <list>
            <ref bean="car1"></ref>
            <ref bean="car2"></ref>
            <ref bean="car3"></ref>
        </list>
    </property>
</bean>

2.将Map注入,采用entry标签,如果是引用类型,采用value-ref属性,如果是字面量(基本数据类型或String类型)采用value属性。

<bean id="car1" class="com.llf.bean.Car">
    <property name="brand" value="宝马"></property>
    <property name="price" value="1000000.00"></property>
    <property name="speed" value="300"></property>
</bean>
<bean id="car2" class="com.llf.bean.Car">
    <property name="brand" value="奔驰"></property>
    <property name="price" value="1500000.00"></property>
    <property name="speed" value="320"></property>
</bean>
<bean id="car3" class="com.llf.bean.Car">
    <property name="brand" value="宾利"></property>
    <property name="price" value="18000000"></property>
    <property name="speed" value="400"></property>
</bean>

<bean id="greatMan" class="com.llf.bean.GreatMan">
    <property name="name" value="马化腾"></property>
    <property name="age" value="40"></property>
    <property name="cars">
        <map>
            <entry key="AA" value-ref="car1"></entry>
            <entry key="BB" value-ref="car2"></entry>
            <entry key="BB" value-ref="car3"></entry>
        </map>
    </property>
</bean>

3.上诉的中的集合,都不可以将被其他的对象所引用,那么List和Map对象如何定义成其他Bean都能用的呢?通过id就可以,当作正常的bean去理解

<util:list id="carsList" value-type="com.llf.bean.Car">
    <ref bean="car1"></ref>
    <ref bean="car2"></ref>
    <ref bean="car3"></ref>
</util:list>

<util:map id="carsMap" key-type="java.lang.String" value-type="com.llf.bean.Car">
    <entry key="AA" value-ref="car1"></entry>
    <entry key="BB" value-ref="car2"></entry>
    <entry key="CC" value-ref="car3"></entry>
</util:map>

 4.常用的Properties也是可以在xml中配置的,既可以配置在bean中,也可以独立于bean外被引用 ,区别在于是否加util和id

<util:properties id="dataSource">
    <prop key="driverClass" >com.mysql.jdbc.Driver</prop>
    <prop key="username">root</prop>
    <prop key="password">123456</prop>
    <prop key="url">jdbc:mysql://localhost:3306/test</prop>
</util:properties>

 

SpringIOC容器-xml配置bean-反射构造器-属性注入(集合类型)

原文:https://www.cnblogs.com/linglongfang/p/12721017.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!