1. package com.yiibai.common;
    2. public class Developer {
    3. private Language language;
    4. //autowire by constructor
    5. public Developer(Language language) {
    6. this.language = language;
    7. }
    8. //...
    9. }
    1. package com.yiibai.common;
    2. public class Language {
    3. private String name;
    4. //...
    5. }

    Spring装配

    1. 第一种
    2. <bean id="developer" class="com.yiibai.common.Developer">
    3. <constructor-arg>
    4. <ref bean="language" />
    5. </constructor-arg>
    6. </bean>
    7. <bean id="language" class="com.yiibai.common.Language" >
    8. <property name="name" value="Java" />
    9. </bean>
    10. 第二种
    11. <bean id="developer" class="com.yiibai.common.Developer" autowire="constructor" />
    12. <bean id="language" class="com.yiibai.common.Language" >
    13. <property name="name" value="Java" />
    14. </bean>