日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

springboot一個(gè)接口多個(gè)實(shí)現(xiàn)類的調(diào)用方式

 wwq圖書世界 2024-01-23 發(fā)布于山東

1、指定限定名注入實(shí)現(xiàn)類

1.1、定義一個(gè)接口

  1. public interface AnimalService {
  2. public void sound();
  3. }

1.2、創(chuàng)建多個(gè)實(shí)現(xiàn)類

  1. @Service("s1")
  2. public class CatService implements AnimalService {
  3. @Override
  4. public void sound() {
  5. System.out.println("喵喵喵");
  6. }
  7. }
  1. @Service()
  2. public class DogService implements AnimalService {
  3. @Override
  4. public void sound() {
  5. System.out.println("汪汪汪");
  6. }
  7. }
  1. @Service("s3")
  2. public class CattleService implements AnimalService {
  3. @Override
  4. public void sound() {
  5. System.out.println("汪汪汪");
  6. }
  7. }

1.3、指定限定名注入實(shí)現(xiàn)類

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class Main {
  4. @Autowired
  5. @Qualifier(value = "s1")
  6. AnimalService animalService1; //正常啟動(dòng)
  7. //沒有指定bean注入名字的,使用該類首字符小寫的bean的名字
  8. //使用默認(rèn)的
  9. @Resource(name = "dogService")
  10. AnimalService animalService2; //正常啟動(dòng)
  11. //通過@Resource注入,根據(jù)@Service指定的名稱區(qū)分
  12. @Resource(name = "s3")
  13. AnimalService animalService3; //正常啟動(dòng)
  14. @Test
  15. public void test1() {
  16. animalService1.sound();
  17. animalService2.sound();
  18. animalService3.sound();
  19. }
  20. }

 2、Map名注入實(shí)現(xiàn)類

2.1、定義一個(gè)接口

  1. public interface AnimalService {
  2. public void sound();
  3. }

2.2、創(chuàng)建多個(gè)實(shí)現(xiàn)類

  1. @Service("s1")
  2. public class CatService implements AnimalService {
  3. @Override
  4. public void sound() {
  5. System.out.println("喵喵喵");
  6. }
  7. }
  1. @Service()
  2. public class DogService implements AnimalService {
  3. @Override
  4. public void sound() {
  5. System.out.println("汪汪汪");
  6. }
  7. }

2.3、枚舉

  1. public enum AnimalTypeEnum {
  2. DOG(1, "狗狗", "dogService"),
  3. CAT(2, "貓咪", "catService");
  4. public Integer code;
  5. public String msg;
  6. public String service;
  7. public static AnimalTypeEnum getAnimalTypeEnum (Integer code) {
  8. return (AnimalTypeEnum )Arrays.stream(values()).filter((item) -> {
  9. return item.code.equals(code);
  10. }).findFirst().orElseThrow(() -> {
  11. return new BusinessException("biz animal type is not exist");
  12. });
  13. }
  14. private AnimalTypeEnum (final Integer code, final String msg, final String service) {
  15. this.code = code;
  16. this.msg = msg;
  17. this.service = service;
  18. }
  19. }

2.4、指定限定名注入實(shí)現(xiàn)類

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class Main {
  4. @Autowired
  5. Map<String, AnimalService> animalServiceMap;
  6. @Test
  7. public void test1() {
  8. String service = AnimalTypeEnum.getAnimalTypeEnum(1).service;
  9. AnimalService animalService = animalServiceMap.get(service);
  10. animalService.sound();
  11. }
  12. }

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多