Spring注解方式自动装配(autowiring)

Scroll Down

使用@Autowired注解即可实现自动装配

xml文件如下

<?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:contetxt="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <contetxt:annotation-config/>
    <bean id="spellChecker" class="com.buaoy.Editor.SpellChecker"/>
    <bean id="textEditor" class="com.buaoy.Editor.TextEditor"/>
</beans>

定义SpellChecker类和TextEditor类

public class SpellChecker {
    public SpellChecker(){
        System.out.println("使用SpellChecker构造器..." );
    }
    public void checkSpelling(){
        System.out.println("检查拼写中..." );
    }
}
public class TextEditor {
    private SpellChecker spellChecker;


    public TextEditor() {
        System.out.println("使用TextEditor构造器..." );
    }

    public SpellChecker getSpellChecker() {
        return spellChecker;
    }

    public void setSpellChecker(SpellChecker spellChecker) {
        this.spellChecker = spellChecker;
    }

    public void spellCheck(){
        spellChecker.checkSpelling();
    }
}

在set方法上使用@autowired

@Autowired
public void setSpellChecker(SpellChecker spellChecker) {
   this.spellChecker = spellChecker;
}

image.png

在属性上使用@autowired

public class TextEditor {
    @Autowired
    private SpellChecker spellChecker;


    public TextEditor() {
        System.out.println("使用TextEditor构造器..." );
    }

    public SpellChecker getSpellChecker() {
        return spellChecker;
    }
    
    public void spellCheck(){
        spellChecker.checkSpelling();
    }
}

此时不需要set方法,运行结果与上面一致

在构造器上使用@autowired

public class TextEditor {
    private SpellChecker spellChecker;

    @Autowired
    TextEditor(SpellChecker spellChecker) {
        this.spellChecker = spellChecker;
    }

    public TextEditor() {
        System.out.println("使用TextEditor构造器...");
    }

    public SpellChecker getSpellChecker() {
        return spellChecker;
    }

    public void spellCheck() {
        spellChecker.checkSpelling();
    }
}

image.png
此时没有使用TextEditor的无参构造器

其他情况

情况一(@autowired(required=false))

删除掉

<bean id="spellChecker" class="com.buaoy.Editor.SpellChecker"/>

重新运行
image.png
可以看到报错了,因为Spring找不到spellChecker对应的bean,可以在autowired后加上require=false解决这个问题

public class TextEditor {
    private SpellChecker spellChecker;

    @Autowired(required = false)
    TextEditor(SpellChecker spellChecker) {
        this.spellChecker = spellChecker;
    }

    public TextEditor() {
        System.out.println("使用TextEditor构造器...");
    }

    public SpellChecker getSpellChecker() {
        return spellChecker;
    }

    public void spellCheck() {
        spellChecker.checkSpelling();
    }
}

image.png
此时调用了TextEditor的无参构造器

情况二(@Qualifier)

当创建多个相同类型的bean并且只想将其中一个与属性关联时,可以使用@Qualifier注解

Student类

package com.buaoy.people;

public class Student {
    private int age;
    private String name;
    public Student(int age, String name){
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }
}

Profile类

package com.buaoy.people;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Profile {
    @Autowired
    @Qualifier("student2")
    private Student student;

    @Override
    public String toString() {
        return "Profile{" +
                "name=" + student.getName() + " " +
                "age=" + student.getAge() +
                '}';
    }
}

xml文件

    <bean id="student1" class="com.buaoy.people.Student" c:age="18" c:name="Jack"/>
    <bean id="student2" class="com.buaoy.people.Student" c:age="20" c:name="Mike"/>
    <bean id="profile" class="com.buaoy.people.Profile"/>

运行结果
image.png
可以看到选择的是student2的信息,因为注解@Qualifier选择的是student2