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="liaohang"/>
        <property name="age" value="26"/>
    </bean>

    <bean id="p2" class="com.Person" autowire-candidate="false">
        <property name="name" value="ZhangSan"/>
        <property name="age" value="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="liaohang"/>
        <property name="age" value="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="liaohang"/>
        <property name="age" value="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

 

 

 2.自动装配注解使用

  

@Configuration
@ComponentScan
public class Config1 {

}

@Component
public class Leader {
    public String lead ="hello leader";
}

@Component
@PropertySource("p.properties")
public class Person {

    @Value("${name}")
    public String name;

    @Autowired
    public Leader leader;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public static void main(String[] args){
    ApplicationContext context = new AnnotationConfigApplicationContext(Config1.class);
    Person p = context.getBean("person",Person.class);
    System.out.println(p.leader.lead);
}

  

打印:

  hello leader

posted @ 2017-03-14 20:13  lh_cn  阅读(201)  评论(0编辑  收藏  举报