Spring中@Autowired註解的使用和注入規則

         作爲一個Spring開發者對@Autowired註解必定是非常瞭解了, 顧名思義自動裝配,應該是Spring會自動將我們標記爲@Autowired的元素裝配好,與其猜測不如看看它的定義:

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {

  boolean required() default true;

}

      很明顯這個註解可以用到構造器,變量域,方法,註解類型和方法參數上。文檔上這樣描述:將一個構造器,變量域,setter方法,config方法標記爲被Spring DI 工具自動裝配。換句話說,在Spring創建bean的過程中,會爲這個bean中標有@Autowired註解的構造器,變量域,方法和方法參數中自動注入我們需要的已經在Spring IOC容器裏面的bean,,而無需我們手動完成,並且注入的bean都是單實例,也就是在兩個bean中都依賴第三個bean,那麼這兩個bean中注入的第三個bean會是同一個bean(JVM中指向的地址相同)。

      在@Autowired註解裏面有一個required屬性,該屬性默認爲true,當爲true時,表示去Spring IOC中查找相應的bean,如果找不到,則會報錯,如果爲false時,表示去Spring IOC中查找相應的bean,如果找不到,則直接忽略,不再進行注入。

      @Autowired註解的注入規則:默認按照類型進行注入,如果IOC容器中存在兩個及以上的相同類型的bean時,根據bean的名稱進行注入,如果沒有指定名稱的bean,則會報錯。 

       可以使用@Qualifier("wheel")來使用指定id的bean,也可以在注入bean時,添加@Primary註解,優先添加一個bean,其規則如下:

      如果指定添加了@Qualifier("wheel")則按照指定的bean id進行添加(優先級最高),找不到則直接報錯。如果沒有添加@Qualifier而添加了@Primary註解,則首先添加標註了@Primary註解的bean。當即存在@Qualifier註解也存在@Primary註解註解,則按照@Qualifier指定的bean id注入,找不到直接報錯。

      很多人java開發者都知道@Autowired註解,但是真正用的好的也不多(反正系統的學習Spring之前我是不知道的),那下面讓我們來看一下@Autowired的用法:

      1.使用在變量域上面

       這個相信大家都已經清楚了,Spring會幫我們注入我們想要的bean,看下面的例子:

package it.cast.circularDependency;

@Component
public class Wheel {

}

@Component
public class Car {

    @Autowired
    private Wheel wheel2;

    public Wheel getWheel() {
        return wheel2;
    }

    public void setWheel(Wheel wheel2) {
        this.wheel2 = wheel2;
    }
}

@ComponentScan({"it.cast.circularDependency"})
public class AutowiredConfig {

}

     下面進行測試,打印的結果顯示可以拿到Wheel類,說明@Autowired註解在IOC容器中只有一個類型的bean時,按照類型進行注入。

    @Test
    public void AutowiredConfigTest(){
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(AutowiredConfig.class);

        Car bean = context.getBean(Car.class);
        System.out.println(bean.getWheel());
    }

//打印結果:
//   it.cast.circularDependency.Wheel@3eb25e1a

      下面看一下當IOC容器中有兩個Wheel類型的bean時的情況,改造Wheel類,增加一個屬性標識用於記錄向Car類中注入的哪個Wheel的bean,在AutowiredConfig配置類中添加一個bean,bean的名稱默認爲方法名,也就是wheel1。

@Component
public class Wheel {
    private int num = 2;   //通過包掃描的方式注入的bean的num值爲2

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}

@Component
public class Car {

    @Autowired
    private Wheel wheel3;//將變量名改成wheel3,IOC容器中Wheel類型的bean的名稱只有wheel和wheel1

    public Wheel getWheel() {
        return wheel3;
    }

    public void setWheel(Wheel wheel3) {
        this.wheel3 = wheel3;
    }
}


@Configuration
@ComponentScan({"it.cast.circularDependency"})
public class AutowiredConfig {

    @Bean
    public Wheel wheel1(){ 
        Wheel wheel = new Wheel();//通過配置類注入bean的方式num值爲0
        wheel.setNum(0);
        return wheel;
    }
}

      這時在Spring IOC中有兩個Wheel類型的bean了,Car在注入Wheel類型的bean時,會根據變量名wheel3去找,也就是說會去找類型爲Wheel,名稱爲wheel3的bean,顯然是找不到的,也就會報錯。

Exception encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'car': 
Unsatisfied dependency expressed through field 'wheel3'; 
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
No qualifying bean of type 'it.cast.circularDependency.Wheel' available: 
expected single matching bean but found 2: wheel,wheel1

org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'car': Unsatisfied dependency expressed through field 'wheel3'; 
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
No qualifying bean of type 'it.cast.circularDependency.Wheel' available: 
expected single matching bean but found 2: wheel,wheel1

      上面爲報錯的日誌打印,大致意思說的在創建名稱爲car的bean時,不能爲變量域wheel3完成屬性注入,因爲找到了兩個bean,分別是wheel和wheel1。

     如果我們把Car中的wheel3換成wheel就可以完成注入了,而且注入的bean是通過包掃描注入IOC的bean:

@Component
public class Wheel {
    private int num = 2;   //通過包掃描的方式注入的bean的num值爲2

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}

@Component
public class Car {

    @Autowired
    private Wheel wheel;//將變量名改成wheel1,IOC容器中Wheel類型的bean的名稱只有wheel和wheel1

    public Wheel getWheel() {
        return wheel;
    }

    public void setWheel(Wheel wheel3) {
        this.wheel = wheel;
    }
}


@Configuration
@ComponentScan({"it.cast.circularDependency"})
public class AutowiredConfig {

    @Bean
    public Wheel wheel1(){ 
        Wheel wheel = new Wheel();//通過配置類注入bean的方式num值爲0
        wheel.setNum(0);
        return wheel;
    }
}

      在測試類中打印num值看看注入的是哪個bean:

    @Test
    public void AutowiredConfigTest(){
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(AutowiredConfig.class);

        Car bean = context.getBean(Car.class);
        System.out.println(bean.getWheel().getNum());
    }

   //打印結果:
   //    2

      那麼就驗證了上面所說的注入規則:默認按照類型進行注入,如果IOC容器中存在兩個及以上的相同類型的bean時,根據bean的名稱進行注入,如果沒有指定名稱的bean,則會報錯。 

      @Autowired註解使用在變量域中還可以解決循環依賴的問題,循環依賴問題就是A對象中注入了B對象,B對象中注入了A對象,循環依賴在面試Spring這一塊的知識應該經常會被問題,關於循環依賴的問題,在後面的博客中會更新。

      2.@Autowired註解使用在構造器上面

       @Autowired使用在構造器上面有幾條需要特別注意的點:

          1.@Autowired標註在構造器上面不能解決循環依賴構造的問題

          2.@Autowired可以標註在同一個類的多個構造器上面,但是required屬性必須都爲false,當required有一個爲true時,不允許其他構造器標有@Autowired註解,即使required屬性爲false也不行。

@Component
public class A {
    private B b;
    private C c;

    @Autowired
    public A(B b, C c) {
        System.out.println("b=" + b + ", c=" + c);
        this.b = b;
        this.c = c;
    }
}

@Component
public class B {

}

@Component
public class C {
}

//打印結果:
//  b=it.cast.circularDependency.B@68e965f5, c=it.cast.circularDependency.C@6f27a732

       @Autowired標註在構造器上面,在B創建的過程中,會去Spring IOC中拿到需要的注入的bean,完成B的創建,其實在只有一個構造器的情況中,@Autowired可以不加,因爲Spring內部有自動推斷構造器的能力,這個如果想了解自動推斷構造器的同學可以自行百度(實在是太難了,一臉矇蔽)。

       下面看一個構造器循壞依賴的案例:

@Component
public class C {
    private B b;

    public C(B b) {
        this.b = b;
    }
}

@Component
public class B {
    private C c;

    public B(C c) {
        this.c = c;
    }
}

       Spring目前不能解決構造器的循環依賴,所以在項目中使用的時候要格外注意一下,錯誤日誌:

org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'a' defined in file
 [E:\IdeaProjects\javaBasis\spring\target\classes\it\cast\circularDependency\A.class]:
 Unsatisfied dependency expressed through constructor parameter 0; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'b' defined in file
 [E:\IdeaProjects\javaBasis\spring\target\classes\it\cast\circularDependency\B.class]: 
Unsatisfied dependency expressed through constructor parameter 0; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'c' defined in file 
[E:\IdeaProjects\javaBasis\spring\target\classes\it\cast\circularDependency\C.class]: 
Unsatisfied dependency expressed through constructor parameter 0; 
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: 
Error creating bean with name 'b': 
Requested bean is currently in creation: Is there an unresolvable circular reference?

    下面看一下關於多個@Autowired標註的構造器的案例:

@Component
public class A {
    private B b;
    private C c;

    @Autowired(required = false)
    public A(B b) {
        this.b = b;
    }

    @Autowired
    public A(B b, C c) {
        System.out.println("b=" + b + ", c=" + c);
        this.b = b;
        this.c = c;
    }
}

@Component
public class B {

}

@Component
public class C {

}

        上面已經說到,如果@Autowired註解的屬性required爲true時,不允許再出現其他構造器上面標有@Autowired註解(@Autowired註解的required默認爲true,所以上面的會報錯),錯誤日誌爲:

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'a': Invalid autowire-marked constructors: 
[public it.cast.circularDependency.A(it.cast.circularDependency.B)]. 
Found constructor with 'required' Autowired annotation: 
public it.cast.circularDependency.A(it.cast.circularDependency.B,it.cast.circularDependency.C)

       使用下面的寫法就不會出現錯誤了,Spring支持多個構造器有@Autowired註解,但是required屬性必須都是false

@Component
public class A {
    private B b;
    private C c;

    @Autowired(required = false) 
    public A(B b) {
        this.b = b;
    }

    @Autowired(required = false)
    public A(B b, C c) {
        System.out.println("b=" + b + ", c=" + c);
        this.b = b;
        this.c = c;
    }
}

@Component
public class B {

}

@Component
public class C {

}

     關於@Autowired標註在方法上就不多介紹,會首先拿到方法的參數列表,然後根據上面所說的注入規則去Spring IOC中找相應的bean。

發佈了221 篇原創文章 · 獲贊 30 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章