<bean id="panda" class="com.tw511.common.Panda" autowire="autodetect" /> <bean id="kungfu" class="com.tw511.common.KungFu" > <property name="name" value="Tai Ji" /> </bean>
package com.tw511.common; public class Panda { private KungFu kungfu; public Panda(KungFu kungfu) { System.out.println("autowiring by constructor"); this.kungfu = kungfu; } public KungFu getKungfu() { return kungfu; } public void setKungfu(KungFu kungfu) { System.out.println("autowiring by type"); this.kungfu = kungfu; } //... }
輸出
autowiring by type Person [kungfu=Language [name=Tai Ji]]
package com.tw511.common; public class Panda { private KungFu kungfu; public KungFu getKungfu() { return kungfu; } public void setKungfu(KungFu kungfu) { System.out.println("autowiring by type"); this.kungfu = kungfu; } //... }
輸出
autowiring by type Person [kungfu=Language [name=Tai Ji]]