spring如何動(dòng)態(tài)指定具體實(shí)現(xiàn)類
在寫接口實(shí)現(xiàn)時(shí),有時(shí)會(huì)有多個(gè)實(shí)現(xiàn)類。這篇文章介紹在調(diào)用時(shí)通過(guò)傳入字符串來(lái)指定具體的實(shí)現(xiàn)類。
一.接口與實(shí)現(xiàn)類:
// 接口 public interface ServiceInterface { public void method(); } // 具體兩個(gè)實(shí)現(xiàn)類 @Service("aService") public class AServiceImpl implements ServiceInterface { @Override public void method() { System.out.println("the impl is A"); } @Override public String toString() { return "A"; } } @Service("bService") public class BServiceImpl implements ServiceInterface { @Override public void method() { System.out.println("the impl is B"); } @Override public String toString() { return "B"; } }
在實(shí)現(xiàn)類中重寫了toString() 方法,可以自定義字符串,當(dāng)調(diào)用時(shí)傳入指定的字符串就能獲取到相應(yīng)的bean。
二.register書(shū)寫:
@Service("register") public class Register implements InitializingBean, ApplicationContextAware { private Map<String, ServiceInterface> serviceImplMap = new HashMap<>(); private ApplicationContext applicationContext; // 獲取spring的上下文 @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } // 獲取接口實(shí)現(xiàn)類的所有bean,并按自己定的規(guī)則放入map中 @Override public void afterPropertiesSet() throws Exception { Map<String, ServiceInterface> beanMap = applicationContext.getBeansOfType(ServiceInterface.class); // 以下代碼是將bean按照自己定的規(guī)則放入map中,這里我的規(guī)則是key:service.toString();value:bean // 調(diào)用時(shí),參數(shù)傳入service.toString()的具體字符串就能獲取到相應(yīng)的bean // 此處也可以不做以下的操作,直接使用beanMap,在調(diào)用時(shí),傳入bean的名稱 for (ServiceInterface serviceImpl : beanMap.values()) { serviceImplMap.put(serviceImpl.toString(), serviceImpl); } } public ServiceInterface getServiceImpl(String name) { return serviceImplMap.get(name); } }
三.測(cè)試類:
@Resource Register register; @Test public void testService() { ServiceInterface service = register.getServiceImpl("A"); service.method(); ServiceInterface service2 = register.getServiceImpl("B"); service2.method(); }
運(yùn)行結(jié)果,如圖:
備注:
在spring加載后,獲取applicationContext的方法:
實(shí)現(xiàn)ApplicationContextAware接口的Bean,在Bean加載的過(guò)程中可以獲取到Spring的ApplicationContext,這個(gè)尤其重要,ApplicationContext是Spring應(yīng)用上下文,從ApplicationContext中可以獲取包括任意的Bean在內(nèi)的大量Spring容器內(nèi)容和信息
@Component("informerRegistry") public final class InformerRegistry implements ApplicationContextAware{ private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }
關(guān)于spring常用bean擴(kuò)展接口可參考:http://www.cnblogs.com/xrq730/p/5721366.html
注意:
使用以下方法獲取spring上下文時(shí),會(huì)啟動(dòng)spring。多次寫以下方法,就會(huì)啟動(dòng)多個(gè)spring容器
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Maven多模塊工程Module開(kāi)發(fā)(圖文教程)
這篇文章主要介紹了Maven多模塊工程Module開(kāi)發(fā)(圖文教程),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12Java之Error與Exception的區(qū)別案例詳解
這篇文章主要介紹了Java之Error與Exception的區(qū)別案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09spring boot使用sharding jdbc的配置方式
這篇文章主要介紹了spring boot使用sharding jdbc的配置方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12Spring Cloud Config Client超時(shí)及重試示例詳解
這篇文章主要給大家介紹了關(guān)于Spring Cloud Config Client超時(shí)及重試的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05