使用Spring InitializingBean 接口實(shí)現(xiàn)多態(tài)注入

我們?cè)谌粘i_發(fā)中,有時(shí)候會(huì)遇到需要使用接口實(shí)現(xiàn)多態(tài)的需求,那么在Spring中該如何實(shí)現(xiàn)呢?
Spring為我們提供了InitializingBean 接口,接口中的afterPropertiesSet()方法將在所有的屬性被初始化后調(diào)用,但是會(huì)在init前調(diào)用。
下面我們先來(lái)看一下該接口的定義:

public interface InitializingBean {
    /**
     * Invoked by a BeanFactory after it has set all bean properties supplied
     * (and satisfied BeanFactoryAware and ApplicationContextAware).
     * <p>This method allows the bean instance to perform initialization only
     * possible when all bean properties have been set and to throw an
     * exception in the event of misconfiguration.
     * @throws Exception in the event of misconfiguration (such
     * as failure to set an essential property) or if initialization fails.
     */
    void afterPropertiesSet() throws Exception;
}

想要實(shí)現(xiàn)接口的多態(tài)注入需要進(jìn)行如下的步驟:
首先,定義一個(gè)接口類:

public interface IService {

    Integer getType();

    void print(String str);
}

然后寫兩個(gè)實(shí)現(xiàn)類實(shí)現(xiàn)IService接口,實(shí)現(xiàn)接口中的方法:

@Service
public class IServiceAImpl implements IService{
    @Override
    public Integer getType() {
        return new Integer(1);
    }

    @Override
    public void print(String str) {
        System.out.preintln(str);
    }
}

@Service
public class IServiceBImpl implements IService{
    @Override
    public Integer getType() {
        return new Integer(2);
    }

    @Override
    public void print(String str) {
        System.out.preintln(str);
    }
}

下面是核心的代碼,實(shí)現(xiàn)路由功能:

@Component
public class ServiceRouter implements InitializingBean {

    private static final Map<Integer, IService> routerMap = Maps.newHashMap();

    @Autowired
    private List<IService> iServiceList;

    @Override
    public void afterPropertiesSet() throws Exception {
        for (IService service : iServiceList) {
            routerMap.put(service.getType(), service);
        }
    }

    public IService getService(Integer type) {
        return routerMap.get(type);
    }
}

寫一個(gè)測(cè)試類:

@RestController
public class MyController {

    @Autowired
    private ServiceRouter router;

    @RequestMapping("/test")
    public void test(@RequestParam Integer type) {
        IService service = router.getService(type);
        service.print("hello world");
    }
}

那么現(xiàn)在就可以根據(jù)傳進(jìn)來(lái)的type進(jìn)行動(dòng)態(tài)的調(diào)用實(shí)現(xiàn)類,實(shí)現(xiàn)多態(tài)了。是不是很簡(jiǎn)單。

由于本人水平有限,如有錯(cuò)誤請(qǐng)大家多多包涵~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,544評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,265評(píng)論 6 342
  • 從三月份找實(shí)習(xí)到現(xiàn)在,面了一些公司,掛了不少,但最終還是拿到小米、百度、阿里、京東、新浪、CVTE、樂(lè)視家的研發(fā)崗...
    時(shí)芥藍(lán)閱讀 42,790評(píng)論 11 349
  • CanvasRenderingContext2D.drawImage()是 Canvas 2D API 中的方法,...
    JasonQiao閱讀 514評(píng)論 0 1
  • 燥熱的天氣,總有太多不平 走在煙臺(tái)的大道,純玩團(tuán)幾乎沒(méi)有購(gòu)物團(tuán)太大的坑,旅行社還問(wèn)你行是不行 年少輕狂意氣風(fēng)發(fā),不...
    紫拉加一閱讀 557評(píng)論 3 1

友情鏈接更多精彩內(nèi)容