RichMan类中有一个属性的类型是List<Car>类型
GreatMan类中有一个属性的类型是Map<String,Car>类型
这里不再使用Car类的有参构造方法,因为Car类这里只是为了new对象被RichMan和GreatMan所注入
这里的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>
<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>
<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>
<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