SpringBoot系列之註解@Autowired VS @Qualifier VS @Primary(五)

前言

咱們看到這幾篇內容都是掌握基礎,避免後續咱們作項目時回頭從新複習,因此本節咱們來討論下註解@Autowired和@Qualifier的區別所在。spring

@Autowired VS @Qualifier VS @Primary

首先咱們定義以下一個車輛接口,咱們知道車輛能夠啓動和中止,因此在此接口中咱們定義這兩個方法,以下:springboot

package com.demo.springboot;

public interface Vehicle {
    String start();
    String stop();
}

接下來咱們定義汽車和自行車類來實現上述接口,以下:ide

package com.demo.springboot;

import org.springframework.stereotype.Component;

@Component
public class Bike implements Vehicle {
    @Override
    public String start() {
        return "Bike started";
    }

    @Override
    public String stop() {
        return "Bike stopped";
    }
}
package com.demo.springboot;

import org.springframework.stereotype.Component;

@Component
public class Car implements Vehicle {
    @Override
    public String start() {
        return  "Car started";
    }

    @Override
    public String stop() {
        return "Car stopped";
    }
}

接下來咱們定義車輛服務類,並調用車輛接口,以下:函數

package com.demo.springboot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VehicleService {
    
    @Autowired
    private Vehicle vehicle;

    public String start() {
        return vehicle.start();
    }

    public String stop() {
        return vehicle.stop();
    }
}

咱們在經過汽車和自行車實現車輛接口,同時經過添加註解@Component來建立bean,接下來咱們在建立車輛服務類時經過註解@Autowired來獲取Spring IOC容器中的車輛bean,可是此時咱們發現車輛接口是不明確的,咱們不知道此接口的具體實現究竟是汽車仍是自行車,因此編譯器自動報錯以下:this

這個時候註解@Qualifier就派上用場了,經過此單詞解釋也瞭然,意爲限定:若是有多個相同類型的bean,則@Qualifier批註用於解決自動註解@Autowired衝突。 因此咱們須要經過註解@Qualifier來明確限定該接口的實例時汽車仍是自行車類,這裏咱們限定爲自行車類,以下:spa

package com.demo.springboot;

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

@Service
public class VehicleService {

    @Autowired
    @Qualifier("bike")
    private Vehicle vehicle;

    public String start() {
        return vehicle.start();
    }

    public String stop() {
        return vehicle.stop();
    }
}

註解@Autowired既能夠做用於字段,也能夠做用於構造函數,使用註解@Autowired做用於構造函數時,在Spring 4.3版本後(若是我沒記錯的話)能夠省略註解@Autowired,因此咱們如上能夠等同以下:code

package com.demo.springboot;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class VehicleService {
    
    private Vehicle vehicle;

    public VehicleService(@Qualifier("bike") Vehicle vehicle) {
        this.vehicle = vehicle;
    }

    public String start() {
        return vehicle.start();
    }

    public String stop() {
        return vehicle.stop();
    }
}

如上是咱們在使用接口具體實例時使用註解@Qualifier限定,要是針對車輛接口的具體實現汽車和自行車類,咱們有一個老是想注入的實現,也就是說在接口具體實現源頭就指定而非調用時指定,此時咱們應該怎麼辦呢?此時咱們可使用註解@Primary,以下:blog

package com.demo.springboot;

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Component
@Primary
public class Bike implements Vehicle {
    @Override
    public String start() {
        return "Bike started";
    }

    @Override
    public String stop() {
        return "Bike stopped";
    }
}
package com.demo.springboot;

import org.springframework.stereotype.Service;

@Service
public class VehicleService {

    private Vehicle vehicle;

    public VehicleService(Vehicle vehicle) {
        this.vehicle = vehicle;
    }

    public String start() {
        return vehicle.start();
    }

    public String stop() {
        return vehicle.stop();
    }
}

想必經過如上簡單的例子咱們明白瞭如題三者的使用,咱們一樣來下一個結論:@Autowired註解使咱們可以將依賴項注入bean,當能夠按類型可以惟一地找到每一個bean時,它無需附加配置就能夠很好地工做,可是當咱們能夠注入多個bean時,咱們必須使用註解@Qualifier按名稱或註解@Primary指定主體來幫助Spring找到所需的具體實現。 接口

總結

本節簡短的內容咱們到此結束,關於一些基本註解的使用和區別到這裏就已結束,後續遇到其餘的繼續經過博文補充,下一節咱們將進入Web的旅行,感謝您的閱讀,咱們下節見。編譯器

相關文章
相關標籤/搜索