[Spring]5.自动装配
建立实体类:

一个人有两个宠物

编写beans.xml
1.正常的手动装配:
<bean id="cat" class="com.wang.pojo.Cat"/>
<bean id="dog" class="com.wang.pojo.Dog"/>
<bean id="people" class="com.wang.pojo.People"
p:cat-ref="cat" p:dog-ref="dog" p:name="wxz"></bean>
结果:

2.自动装配:
1.ByName
<bean id="people2" class="com.wang.pojo.People" p:name="wxz" autowire="byName"></bean>
结果正常。可如果bean的名字和set方法的名字不一致,则无法自动装配:

2.ByType
<bean id="people2" class="com.wang.pojo.People" p:name="wxz" autowire="byType"></bean>
如果xml中有多种相同的Type,则无法实现自动装配


浙公网安备 33010602011771号