spring之使用命名空间p来简化bean的配置
一般情况下,我们是这么配置bean的:
<bean id="car1" class="com.gong.spring.beans.Car">
<property name="name" value="baoma"></property>
</bean>
<bean id="car2" class="com.gong.spring.beans.Car">
<property name="name" value="benchi"></property>
</bean>
<bean id="car3" class="com.gong.spring.beans.Car">
<property name="name" value="binli"></property>
</bean>
<bean id="student" class="com.gong.spring.beans.Student">
<property name="name" value="tom"></property>
<property name="age" value="12"></property>
<property name="score" value="98.00"></property>
<property name="cars" ref="cars">
</property>
</bean>
<util:list id="cars">
<ref bean="car1"/>
<ref bean="car2"/>
<ref bean="car3"/>
</util:list>说明:cars是公用的集合Bean,Student里有name、age、score以及类型为List<Car>的car属性。
在引入了命名空间之后,我们就可以这么进行配置了:
<bean id="car1" class="com.gong.spring.beans.Car" p:name="baoma"></bean>
<bean id="car2" class="com.gong.spring.beans.Car" p:name="benchi"></bean>
<bean id="car3" class="com.gong.spring.beans.Car" p:name="binli"></bean>
<bean id="student" class="com.gong.spring.beans.Student"
p:name="tom" p:age="12" p:score="98.00" p:cars-ref="cars"></bean>
<util:list id="cars">
<ref bean="car1"/>
<ref bean="car2"/>
<ref bean="car3"/>
</util:list>相较于原来的,代码简洁了很多。
相关推荐
杜鲁门 2020-11-05
与卿画眉共浮生 2020-10-14
lukezhong 2020-10-14
tangxiong0 2020-09-03
YangHuiLiang 2020-08-06
Sweetdream 2020-08-03
编程点滴 2020-07-29
smalllove 2020-07-27
iconhot 2020-07-05
XGQ 2020-07-04
MicroBoy 2020-07-04
itjavashuai 2020-07-04
zmysna 2020-07-04
willluckysmile 2020-06-29
CoderBoy 2020-06-28
爱莲说 2020-06-26
itjavashuai 2020-06-25
HappyHeng 2020-06-21
smalllove 2020-06-14