springboot一个接口多个实现类的调用方式

本文详细介绍了在SpringBoot应用中如何通过接口和Map进行指定名称的注入,包括使用@Service注解、@Qualifier、AnimalService接口以及AnimalTypeEnum枚举来管理不同服务实例的注入过程。
摘要由CSDN通过智能技术生成

1、指定限定名注入实现类

1.1、定义一个接口

public interface AnimalService {
    public void sound();
}

1.2、创建多个实现类

@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、指定限定名注入实现类

@RunWith(SpringRunner.class)
@SpringBootTest
public class Main {
    @Autowired
    @Qualifier(value = "s1")
    AnimalService animalService1;    //正常启动

    //没有指定bean注入名字的,使用该类首字符小写的bean的名字
    //使用默认的
    @Resource(name = "dogService")
    AnimalService animalService2;    //正常启动

    //通过@Resource注入,根据@Service指定的名称区分
    @Resource(name = "s3")
    AnimalService animalService3;    //正常启动

    @Test
   public void test1() {
        animalService1.sound();

        animalService2.sound();

        animalService3.sound();     
    }
}

 2、Map名注入实现类

2.1、定义一个接口

public interface AnimalService {
    public void sound();
}

2.2、创建多个实现类

@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("汪汪汪");
    }
}

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、指定限定名注入实现类

@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();
    }
}

Spring Boot中,我们可以通过编写一个工具类来实现对第三方HTTP接口调用。通常,我们会使用RestTemplate来发送HTTP请求。以下是一个简单的示例: 首先,需要在Spring Boot项目中引入RestTemplate的依赖。在`pom.xml`中添加如下依赖(如果是Maven项目): ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 接下来,创建一个工具类,比如`ThirdPartyApiCaller`,在这个类中注入RestTemplate并提供一个方法来执行HTTP请求: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.*; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; @Component public class ThirdPartyApiCaller { private final RestTemplate restTemplate; @Autowired public ThirdPartyApiCaller(RestTemplate restTemplate) { this.restTemplate = restTemplate; } // 发起GET请求的方法示例 public ResponseEntity<String> get(String url) { ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); return response; } // 发起POST请求的方法示例 public ResponseEntity<String> post(String url, Object requestEntity) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<Object> entity = new HttpEntity<>(requestEntity, headers); ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class); return response; } // 可以根据需要添加更多方法,如PUT、DELETE等 } ``` 在这个类中,`get`方法用于发起GET请求,`post`方法用于发起POST请求。这些方法通过`RestTemplate`的相应方法实现HTTP调用。你也可以根据需要添加其他HTTP方法(如PUT、DELETE等)的调用。 使用此类时,你只需要注入`ThirdPartyApiCaller`这个bean,然后就可以调用相应的方法来进行HTTP调用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值