<!-- person has a property type of class "ability" --> <bean id="person" class="com.zyiz.netmon.Person" autowire="byType" /> <bean id="invisible" class="com.zyiz.netmon.Ability" > <property name="skill" value="Invisible" /> </bean>
package com.zyiz.netmon; public class Person { private Ability ability; //... }
package com.zyiz.netmon; public class Ability { private String skill; //... }
<bean id="person" class="com.zyiz.netmon.Person"> <property name="ability" ref="invisible" /> </bean> <bean id="invisible" class="com.zyiz.netmon.Ability" > <property name="skill" value="Invisible" /> </bean>
输出
Person [ability=Ability [skill=Invisible]]
<bean id="person" class="com.zyiz.netmon.Person" autowire="byType" /> <bean id="invisible" class="com.zyiz.netmon.Ability" > <property name="skill" value="Invisible" /> </bean>
输出
Person [ability=Ability [skill=Invisible]]
<bean id="person" class="com.zyiz.netmon.Person" autowire="byType" /> <bean id="steal" class="com.zyiz.netmon.Ability" > <property name="skill" value="Steal" /> </bean> <bean id="invisible" class="com.zyiz.netmon.Ability" > <property name="skill" value="Invisible" /> </bean>
输出
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: ... No unique bean of type [com.zyiz.netmon.Ability] is defined: expected single matching bean but found 2: [steal, invisible]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.zyiz.netmon.Ability] is defined: expected single matching bean but found 2: [steal, invisible]