国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

spring如何動態指定具體實現類

2019-11-26 13:43:28
字體:
來源:轉載
供稿:網友

在寫接口實現時,有時會有多個實現類。這篇文章介紹在調用時通過傳入字符串來指定具體的實現類。

一.接口與實現類:

// 接口public interface ServiceInterface {  public void method();} // 具體兩個實現類@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";  } }

在實現類中重寫了toString() 方法,可以自定義字符串,當調用時傳入指定的字符串就能獲取到相應的bean。 

二.register書寫:

@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;  }   // 獲取接口實現類的所有bean,并按自己定的規則放入map中  @Override  public void afterPropertiesSet() throws Exception {    Map<String, ServiceInterface> beanMap = applicationContext.getBeansOfType(ServiceInterface.class);    // 以下代碼是將bean按照自己定的規則放入map中,這里我的規則是key:service.toString();value:bean    // 調用時,參數傳入service.toString()的具體字符串就能獲取到相應的bean    // 此處也可以不做以下的操作,直接使用beanMap,在調用時,傳入bean的名稱    for (ServiceInterface serviceImpl : beanMap.values()) {      serviceImplMap.put(serviceImpl.toString(), serviceImpl);    }  }     public ServiceInterface getServiceImpl(String name) {    return serviceImplMap.get(name);  } }

三.測試類:

@ResourceRegister register; @Testpublic void testService() {  ServiceInterface service = register.getServiceImpl("A");  service.method();  ServiceInterface service2 = register.getServiceImpl("B");  service2.method();}

運行結果,如圖:

備注:

在spring加載后,獲取applicationContext的方法:

實現ApplicationContextAware接口的Bean,在Bean加載的過程中可以獲取到Spring的ApplicationContext,這個尤其重要,ApplicationContext是Spring應用上下文,從ApplicationContext中可以獲取包括任意的Bean在內的大量Spring容器內容和信息

@Component("informerRegistry")public final class InformerRegistry implements ApplicationContextAware{   private ApplicationContext applicationContext;   @Override  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {    this.applicationContext = applicationContext;  }}

關于spring常用bean擴展接口可參考:http://www.cnblogs.com/xrq730/p/5721366.html 

注意:

使用以下方法獲取spring上下文時,會啟動spring。多次寫以下方法,就會啟動多個spring容器

復制代碼 代碼如下:
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:META-INF/spring/*.xml");

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彭阳县| 栾城县| 外汇| 桑植县| 红原县| 彝良县| 通河县| 玛曲县| 兴安盟| 高安市| 德州市| 贵港市| 泰安市| 洛南县| 江源县| 潞城市| 万安县| 永川市| 吴堡县| 秦皇岛市| 永泰县| 旺苍县| 中山市| 葫芦岛市| 永平县| 岚皋县| 三原县| 吴忠市| 阿坝| 杭锦旗| 吐鲁番市| 肃北| 筠连县| 闸北区| 平陆县| 冷水江市| 台中县| 胶州市| 胶州市| 宝山区| 民丰县|