首页 > 代码库 > Spring基础(1): 自动装配

Spring基础(1): 自动装配

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 id="pafter" class="com.Person">
        <property name="name" value="http://www.mamicode.com/liaohang"/>
        <property name="age" value="http://www.mamicode.com/26"/>
    </bean>

    <bean id="p2" class="com.Person" autowire-candidate="false">
        <property name="name" value="http://www.mamicode.com/ZhangSan"/>
        <property name="age" value="http://www.mamicode.com/26"/>
    </bean>

   <bean id="g1" class="com.Group" autowire="byType">
   </bean>
</beans>
View Code

 

   这个xml存在两个person bean,为了避免歧义,将p2设置autowire-candidate="false",则容器会自动过滤p2,最终p1被注入到group中。

 

      1.1.2 根据名称自动注入,是指 bean名称与setter方法后缀名称 匹配

技术分享
 <bean id="p1234" class="com.Person">
        <property name="name" value="http://www.mamicode.com/liaohang"/>
        <property name="age" value="http://www.mamicode.com/26"/>
    </bean>
   <bean id="g1" class="com.Group" autowire="byName"/>


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

 

  上面的xml配置和 bean则可以自动注入。

 

     1.13 根据构造函数注入,是按构造函数参数中的类型进行自动注入,与构造函数参数名称无关。

技术分享
 <bean id="p2" class="com.Person" autowire-candidate="false">
        <property name="name" value="http://www.mamicode.com/liaohang"/>
        <property name="age" value="http://www.mamicode.com/26"/>
    </bean>
   <bean id="g1" class="com.Group" autowire="constructor"/>

public class Group {
    public Person pafter;
    public Person getPafter() {
        return pafter;
    }

    public Group(Person p1){
        this.pafter =p1;
    }
}

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());
    }
}
View Code

 

  

 

Spring基础(1): 自动装配