spring 使用util:list节点 提取公共的bean

1. 配置文件

<?xml version="1.0" encoding="UTF-8"?>

<!-- beans节点中的红色部分是我加的,否则报出“The matching wildcard is strict, but no declaration can be found for element 'util:list"错误-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-3.1.xsd">






<bean id="car" class="com.hxzy.model.Car">
<constructor-arg type="java.lang.String">
<value>宝马</value>
</constructor-arg>
<constructor-arg type="java.lang.String">
<value>天津</value>
</constructor-arg>
<constructor-arg type="int">
<value>200</value>
</constructor-arg>
</bean>


<bean id="car2" class="com.hxzy.model.Car">
<constructor-arg type="java.lang.String">
<value>奔驰</value>
</constructor-arg>
<constructor-arg type="java.lang.String">
<value>北京</value>
</constructor-arg>
<constructor-arg type="int">
<value>220</value>
</constructor-arg>
</bean>


<bean id="person2" class="com.hxzy.model.Person">
<property name="age" value="58">
</property>
<property name="name" value="老刘"></property>

以下红色部分可以使用这句话替换

<property name="cars" ref="cars">


<property name="cars">
<list>
<ref bean="car" />
<ref bean="car2" />
</list>
</property>

</bean>

<bean id="person4" class="com.hxzy.model.Person">
   <property name="age" value="48"></property>
   <property name="name" value="夏洛"></property>

<!--此处调用了外部的cars bean-->
   <property name="cars" ref="cars"></property> 
</bean> 

<!--此处即为抽出的cars集合bean-->
<util:list id="cars">
   <ref bean="car" />
   <ref bean="car2" />
</util:list>



<bean id="dataSource" class="com.hxzy.model.DataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">1234</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>


</bean>






</beans>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值