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