SpringBoot中一个接口多个实现类注入方式

SpringBoot中一个接口多个实现类注入方式

在开发过程中,我们经常在很多场景中使用面向接口开发业务,自然也就遇到了一个接口多个实现类的场景,那么在SpringBoot中怎么灵活去处理这种场景呢?

下面直接上示例。

下面是简单的一个去第三方查询IP信息的接口实现,可以从不同的服务商那里查询,定一个接口,然后做了两个实现类。


定义接口:


package com.vlistudio.apis.service;

import com.vlistudio.apis.model.IPInfo;

public interface IIPService {

    /**
     * 获取IP详细信息
     * @param ip 被查询的ip地址
     * @return ip详细信息
     */
    IPInfo queryIPDetail(String ip);
}


去【ip-api.com】查询的实现类:


package com.vlistudio.apis.service.impl;

import com.vlistudio.apis.model.IPInfo;
import com.vlistudio.apis.service.IIPService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Component
@ConditionalOnProperty(value = "apis.ip.isp", havingValue = "ipApiCom")
public class IpApiComIPService implements IIPService {

    private static final String API_URL = "http://ip-api.com/json/%s?fields=66846719&lang=zh-CN";

    @Resource
    @Qualifier("vliRestTemplate")
    private RestTemplate restTemplate;

    @Override
    public IPInfo queryIPDetail(String ip) {
        String url = String.format(API_URL, ip);
        ResponseEntity<IPInfo> info = restTemplate.postForEntity(url, null, IPInfo.class);
        return info.getBody();
    }

}


去【pconline.com.cn】查询的实现类:


package com.vlistudio.apis.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.vlistudio.apis.model.IPInfo;
import com.vlistudio.apis.service.IIPService;
import com.vlistudio.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Component
@ConditionalOnProperty(value = "apis.ip.isp", havingValue = "pcOnline")
public class PcOnlineIPService implements IIPService {
    private static final String API_URL = "https://whois.pconline.com.cn/ipJson.jsp?ip=%s&json=true";

    @Resource
    @Qualifier("vliRestTemplate")
    private RestTemplate restTemplate;

    @Override
    public IPInfo queryIPDetail(String ip) {
        String url = String.format(API_URL, ip);
        ResponseEntity<String> res = restTemplate.getForEntity(url, String.class);
        IPInfo info = new IPInfo();
        info.setQuery(ip);

        if (StringUtils.isEmpty(res.getBody())) {
            return info;
        }

        JSONObject obj = JSON.parseObject(res.getBody());
        if (obj == null) {
            return info;
        }

        info.setRegionName(obj.getString("pro"));
        info.setCity(obj.getString("city"));
        info.setAsname(obj.getString("addr"));

        return info;
    }
}


【application.yml】配置文件:


apis:
  ip:
    isp: ipApiCom


业务调用:


@Resource
private IIPService iipService;
......
iipService.queryIPDetail(siteAccess.getIp());
......

微信赞赏