SpringBoot一個(gè)接口多個(gè)實(shí)現(xiàn)類的調(diào)用方式總結(jié)
更新時(shí)間:2024年01月04日 09:57:13 作者:yy_csdn1314
這篇文章主要介紹了SpringBoot一個(gè)接口多個(gè)實(shí)現(xiàn)類的調(diào)用方式,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
1、指定限定名注入實(shí)現(xiàn)類
1.1、定義一個(gè)接口
public interface AnimalService {
public void sound();
}1.2、創(chuàng)建多個(gè)實(shí)現(xiàn)類
@Service("s1")
public class CatService implements AnimalService {
@Override
public void sound() {
System.out.println("喵喵喵");
}
}@Service()
public class DogService implements AnimalService {
@Override
public void sound() {
System.out.println("汪汪汪");
}
}@Service("s3")
public class CattleService implements AnimalService {
@Override
public void sound() {
System.out.println("汪汪汪");
}
}1.3、指定限定名注入實(shí)現(xiàn)類
@RunWith(SpringRunner.class)
@SpringBootTest
public class Main {
@Autowired
@Qualifier(value = "s1")
AnimalService animalService1; //正常啟動(dòng)
//沒有指定bean注入名字的,使用該類首字符小寫的bean的名字
//使用默認(rèn)的
@Resource(name = "dogService")
AnimalService animalService2; //正常啟動(dòng)
//通過@Resource注入,根據(jù)@Service指定的名稱區(qū)分
@Resource(name = "s3")
AnimalService animalService3; //正常啟動(dòng)
@Test
public void test1() {
animalService1.sound();
animalService2.sound();
animalService3.sound();
}
}2、Map名注入實(shí)現(xiàn)類
2.1、定義一個(gè)接口
public interface AnimalService {
public void sound();
}2.2、創(chuàng)建多個(gè)實(shí)現(xiàn)類
@Service("s1")
public class CatService implements AnimalService {
@Override
public void sound() {
System.out.println("喵喵喵");
}
}2.3、枚舉
public enum AnimalTypeEnum {
DOG(1, "狗狗", "dogService"),
CAT(2, "貓咪", "catService");
public Integer code;
public String msg;
public String service;
public static AnimalTypeEnum getAnimalTypeEnum (Integer code) {
return (AnimalTypeEnum )Arrays.stream(values()).filter((item) -> {
return item.code.equals(code);
}).findFirst().orElseThrow(() -> {
return new BusinessException("biz animal type is not exist");
});
}
private AnimalTypeEnum (final Integer code, final String msg, final String service) {
this.code = code;
this.msg = msg;
this.service = service;
}
}2.4、指定限定名注入實(shí)現(xiàn)類
@RunWith(SpringRunner.class)
@SpringBootTest
public class Main {
@Autowired
Map<String, AnimalService> animalServiceMap;
@Test
public void test1() {
String service = AnimalTypeEnum.getAnimalTypeEnum(1).service;
AnimalService animalService = animalServiceMap.get(service);
animalService.sound();
}
}以上就是SpringBoot一個(gè)接口多個(gè)實(shí)現(xiàn)類的調(diào)用方式的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot一個(gè)接口多個(gè)實(shí)現(xiàn)類的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring Boot實(shí)現(xiàn)STOMP協(xié)議的WebSocket的方法步驟
這篇文章主要介紹了Spring Boot實(shí)現(xiàn)STOMP協(xié)議的WebSocket的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
IDEA?code?template配置和參數(shù)方式
這篇文章主要介紹了IDEA?code?template配置和參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教<BR>2024-01-01
SpringBoot項(xiàng)目yml配置文件不自動(dòng)提示解決方案
這篇文章主要介紹了SpringBoot項(xiàng)目配置文件.yaml/.yml文件編寫時(shí)沒有自動(dòng)提示的解決方案,文章通過圖文結(jié)合的方式給大家講解的非常詳細(xì),需要的朋友可以參考下2024-06-06
關(guān)于log4j漏洞修復(fù)解決方案及源碼編譯
Log4j?是Apache為Java提供的日志管理工具。他與System.out.println()的作用相似,用來跟蹤、調(diào)試、維護(hù)程序。這篇文章主要介紹了關(guān)于log4j漏洞修復(fù)解決方案及源碼編譯,需要的朋友可以參考下2021-12-12
IDEA創(chuàng)建MyBatis配置文件模板的方法步驟
這篇文章主要介紹了IDEA創(chuàng)建MyBatis配置文件模板的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

