Spring Boot 中的 @Qualifier 註解用於解決當您有多個相同類型的 Bean 但想要注入特定的一個時出現的歧義。當存在多個候選者時,它可以幫助 Spring 確定應該自動組裝哪個 bean。
以下是所有 @Qualifier 有用的常見場景,並附有範例:
場景 1:相同類型的多個 Bean
您可能有多個相同類型的 Bean,並且您想要注入特定的一個。
範例:
import org.springframework.stereotype.Component; @Component public class Dog implements Animal { @Override public String sound() { return "Bark"; } } @Component public class Cat implements Animal { @Override public String sound() { return "Meow"; } }
這裡,Dog 和 Cat 都實作了 Animal 介面。
@Qualifier 的用法:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @Service public class AnimalService { private final Animal animal; @Autowired public AnimalService(@Qualifier("cat") Animal animal) { this.animal = animal; } public String getAnimalSound() { return animal.sound(); } }
在此範例中,@Qualifier("cat") 註解指定應將 Cat bean 注入到 AnimalService 中。如果沒有 @Qualifier,Spring 會因歧義而拋出異常。
場景 2:將 @Qualifier 與主要和次要 Bean 一起使用
有時,您可能有一個「主要」bean 和其他不常用的 bean,但您仍然希望能夠使用 @Qualifier 注入特定的 bean。
範例:
@Component @Primary public class Dog implements Animal { @Override public String sound() { return "Bark"; } } @Component public class Cat implements Animal { @Override public String sound() { return "Meow"; } }
@primary 註解確保預設注入 Dog。但你仍然可以使用@Qualifier來注入Cat。
使用@Qualifier來覆蓋@primary:
@Service public class AnimalService { private final Animal animal; @Autowired public AnimalService(@Qualifier("cat") Animal animal) { this.animal = animal; } public String getAnimalSound() { return animal.sound(); } }
在本例中,儘管 Dog 被標記為 @primary,但由於 @Qualifier 註解,Cat bean 仍被注入。
場景 3:有建構子注入和欄位注入的@Qualifier
@Qualifier 可以與基於建構函式和基於欄位的注入一起使用。
範例:使用 @Qualifier 進行欄位注入:
@Service public class AnimalService { @Autowired @Qualifier("dog") private Animal animal; public String getAnimalSound() { return animal.sound(); } }
在這種情況下,@Qualifier("dog") 確保將 Dog bean 注入 AnimalService 中。
場景 4:帶有方法參數注入的@Qualifier
透過方法參數注入依賴時也可以使用@Qualifier。
範例:
@Service public class AnimalService { private Animal animal; @Autowired public void setAnimal(@Qualifier("dog") Animal animal) { this.animal = animal; } public String getAnimalSound() { return animal.sound(); } }
這裡,@Qualifier("dog") 確保透過 setter 方法注入 Dog bean。
場景 5:帶有自訂註解的 @Qualifier
您可以建立自訂限定符以避免硬編碼 Bean 名稱,讓程式碼更乾淨且更易於維護。
範例:自訂限定符:
建立自訂限定符:
import org.springframework.beans.factory.annotation.Qualifier; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Qualifier @Retention(RetentionPolicy.RUNTIME) public @interface DogQualifier { }
應用自訂限定符:
@Component @DogQualifier public class Dog implements Animal { @Override public String sound() { return "Bark"; } } @Component public class Cat implements Animal { @Override public String sound() { return "Meow"; } }
使用自訂限定符注入:
@Service public class AnimalService { private final Animal animal; @Autowired public AnimalService(@DogQualifier Animal animal) { this.animal = animal; } public String getAnimalSound() { return animal.sound(); } }
在這個例子中,@DogQualifier用來指定要注入哪個bean,而不是使用@Qualifier("dog")。
場景 6:集合中的 @Qualifier
在自動組裝 Bean 集合時可以使用 @Qualifier,以確保只注入特定的 Bean。
範例:
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component @Qualifier("domestic") public class Dog implements Animal { @Override public String sound() { return "Bark"; } } @Component @Qualifier("domestic") public class Cat implements Animal { @Override public String sound() { return "Meow"; } } @Component public class Lion implements Animal { @Override public String sound() { return "Roar"; } }
與集合一起使用:
@Service public class AnimalService { private final List<animal> animals; @Autowired public AnimalService(@Qualifier("domestic") List<animal> animals) { this.animals = animals; } public void printAnimalSounds() { animals.forEach(animal -> System.out.println(animal.sound())); } } </animal></animal>
在此範例中,僅注入 Dog 和 Cat beans,因為它們被標記為 @Qualifier("domestic")。
摘要:
當存在多個相同類型的候選人時,@Qualifier 有助於注入特定的 beans。
它用於建構函數注入、欄位注入、方法注入、自訂限定符甚至集合等場景。
透過了解這些場景,您可以有效地使用 @Qualifier 來解決歧義並管理 Spring Boot 應用程式中的 bean 注入。
以上是@Qualifier 註解 Spring Boot 詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Java如何緩解平台特定的問題? Java通過JVM和標準庫來實現平台無關性。 1)使用字節碼和JVM抽像操作系統差異;2)標準庫提供跨平台API,如Paths類處理文件路徑,Charset類處理字符編碼;3)實際項目中使用配置文件和多平台測試來優化和調試。

java'splatformentenceenhancesenhancesmicroservicesharchitecture byferingDeploymentFlexible,一致性,可伸縮性和便攜性。 1)DeploymentFlexibilityAllowsibilityAllowsOllowsOllowSorlowsOllowsOllowsOllowSeStorunonAnyPlatformwithajvM.2)penterencyCrossServAccAcrossServAcrossServiCessImplifififiesDeevelopmentandeDe

GraalVM通過三種方式增強了Java的平台獨立性:1.跨語言互操作,允許Java與其他語言無縫互操作;2.獨立的運行時環境,通過GraalVMNativeImage將Java程序編譯成本地可執行文件;3.性能優化,Graal編譯器生成高效的機器碼,提升Java程序的性能和一致性。

效率testjavaapplicationsforplatformcompatibility oftheSesteps:1)setUpautomatedTestingTestingActingAcrossMultPlatFormSusingCitoolSlikeSlikeJenkinSorgithUbactions.2)contuctualtemualtemalualTesteTESTENRETESTINGINREALHARTWARETOLEALHARDOELHARDOLEATOCATCHISSUSESUSEUSENINCIENVIRENTMENTS.3)schictcross.3)schoscross.3)

Java編譯器通過將源代碼轉換為平台無關的字節碼,實現了Java的平台獨立性,使得Java程序可以在任何安裝了JVM的操作系統上運行。

ByteCodeachievesPlatFormIndenceByByByByByByExecutedBoviratualMachine(VM),允許CodetorunonanyplatformwithTheApprepreprepvm.Forexample,Javabytecodecodecodecodecanrunonanydevicewithajvm

Java不能做到100%的平台獨立性,但其平台獨立性通過JVM和字節碼實現,確保代碼在不同平台上運行。具體實現包括:1.編譯成字節碼;2.JVM的解釋執行;3.標準庫的一致性。然而,JVM實現差異、操作系統和硬件差異以及第三方庫的兼容性可能影響其平台獨立性。

Java通過“一次編寫,到處運行”實現平台獨立性,提升代碼可維護性:1.代碼重用性高,減少重複開發;2.維護成本低,只需一處修改;3.團隊協作效率高,方便知識共享。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Dreamweaver CS6
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

WebStorm Mac版
好用的JavaScript開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境