1.自动装配

   1.1 byType

        1.1.1根据类型自动匹配,若当前没有类型可以注入或者存在多个类型可以注入,则失败。必须要有对于的setter方法

public class Person{
    public String name;
    public int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String toString(){
        return this.name+" "+this.age;
    }
}

public class Group {
    public Person pafter;
    public Person getPafter() {
        return pafter;
    }
    public void setPafte(Person pafter) {
        this.pafter = pafter;
    }
}

public class Main {
    static ApplicationContext context = new ClassPathXmlApplicationContext("a.xml");
    public static void main(String[] args){
        Group g = context.getBean("g1",Group.class);
        System.out.println(g.getPafter().toString());
    }
}


<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean >
        <property name="name" value="liaohang"/>
        <property name="age" value="26"/>
    </bean>

    <bean >
        <property name="name" value="ZhangSan"/>
        <property name="age" value="26"/>
    </bean>

   <bean >
   </bean>
</beans>
View Code

相关文章: