【学习笔记】Bean的自动装配

Bean的自动装配

在之前,我们都是手动去把一个bean的属性给注入值。

所谓自动装配就是spring会在上下文中自动寻找,并给bean自动装配属性

自动装配的三种方式:byName、byType、注解自动装配

【环境】

学校、教师、学生,学校有教师和学生的属性,教师有讲课的方法,学生有听课的方法

package com.wang.pojo;
​
public class School {
    private String name;
    private Teacher teacher;
​
    private Student studnet;
​
    public String getName() {
        return name;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    public Teacher getTeacher() {
        return teacher;
    }
​
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
​
    public Student getStudnet() {
        return studnet;
    }
​
    public void setStudnet(Student studnet) {
        this.studnet = studnet;
    }
}
package com.wang.pojo;
​
public class Student {
    public void Listen(){
        System.out.println("学生来听课");
    }
}
package com.wang.pojo;
​
public class Teacher {
    public void teach(){
        System.out.println("老师来讲课");
    }
}

下面通过自动装配,把教师和学生自动装配给学校bean

 

通过byName

<bean id="teacher" class="com.wang.pojo.Teacher"/>
<bean id="student" class="com.wang.pojo.Student"/>
​
<bean id="school" class="com.wang.pojo.School" autowire="byName">
    <property name="name" value="第一中学"/>
</bean>
public void test01(){
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    School school = (School) context.getBean("school");
    System.out.print(school.getName()+"中的");
    school.getTeacher().teach();
    school.getStudent().listen();
​
}

image-20230213164547001

从上面的例子,我们可以看到,并没有将student和teacher的bean手动装配到school的bean中,而是通过一个autowired=“byName”进行了自动装配。

byName是会自动在容器上下文中寻找,和自己对象set方法的参数名对应的bean的id

也就是在school中的setTeacher和setStudent方法的参数名字相同的beanid所对应的bean,会被自动装配到school的bean上。

这就要求beanid必须要唯一,并且和set方法的参数一致

 

通过byType

与byName 相同,bytype会在容器上下文中寻找和自己的属性类型相同的bean

在上面的例子中,school的两个属性的类型为Teacher和Student,所以会寻找类型为Teahcer和Studnet的bean,然后自动装配

要求是必须保证bean的class唯一,否则就会报错

 

注解实现自动装配

想要使用注解必须完成以下两点:

  1. 导入约束

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

    context的约束

  2. 开启注解的使用

    <context:annotation-config/>

第一个注解 @Autowired

可以加在属性上,加在属性上就可以不写set方法

@Autowired
private Teacher teacher;
@Autowired
private Student student;

也可以加在set方法上

@Autowired首先是判断是否符合byName(属性名和bean的id是否相等),如果符合就自动装配。

如果没有bean的id和该属性名相同就会报错

这时可以使用@Qualifier 这个注解来指定某一个bean

如下,没有一个bean的id和属性的名字相同

<bean id="teacher1" class="com.wang.pojo.Teacher"/>
<bean id="teacher1245" class="com.wang.pojo.Teacher"/>
<bean id="student1111" class="com.wang.pojo.Student"/>
<bean id="student1" class="com.wang.pojo.Student"/>
@Autowired
@Qualifier(value = "teacher1")
private Teacher teacher;
@Autowired
@Qualifier(value = "student1111")
private Student student;

使用@Qualifier(value = "student1111")来指定去装配哪一个bean,前提是该bean的class必须与属性的类型相同。(byType)

@Autowired和@Qualifier一般搭配使用

 

第二个注解@Resource

这个注解是java的注解,也可以实现自动转配

这个注解加在属性上

和@Autowired相同,先是匹配byName,如果找不到就会报错

这时可以给它加一个name,来指定某一个bean,前提是要满足byType

@Resource(name="teacher1")
 private Teacher teacher;
 @Resource(name="student1")
 private Student student;

@Resouse注解相当于融合了@Autowired和@Qualifier

posted @ 2023-02-13 17:52  GrowthRoad  阅读(26)  评论(0编辑  收藏  举报