一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - spring如何動態指定具體實現類

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

2020-06-22 12:40登頂 JAVA教程

這篇文章主要為大家詳細介紹了spring如何動態指定具體實現類,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

一.接口與實現類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 接口
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書寫:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@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);
  }
 
}

三.測試類:

?
1
2
3
4
5
6
7
8
9
10
@Resource
Register register;
 
@Test
public void testService() {
  ServiceInterface service = register.getServiceImpl("A");
  service.method();
  ServiceInterface service2 = register.getServiceImpl("B");
  service2.method();
}

運行結果,如圖:

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

備注:

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

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

?
1
2
3
4
5
6
7
8
9
10
@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");

 

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 美女全身无遮挡 | 水多多凹凸福利视频导航 | 513热点网深夜影院影院诶 | 成年美女黄网站色视频大全免费 | 欧亚精品一区二区三区 | 亚洲男人天堂网址 | 99久久精品免费观看区一 | 99久久免费精品视频 | 欧美作爱福利免费观看视频 | 国产在线精品亚洲第一区香蕉 | 亚洲国产日韩成人综合天堂 | 精品国产一区二区三区在线 | 日韩二区三区 | 麻豆网站在线观看 | 国产精品亚洲片在线va | 日韩特级片| ipx-177绝对领域在线观看 | 成年人免费观看 | 欧美黑人一级片 | 91资源在线视频 | 四虎1515hhcom | 91色资源网在线观看 | 国内精品久久久久久野外 | 国产精品怡红院永久免费 | 丝袜足控免费网站xx动漫漫画 | 国产清纯女高中生在线观看 | 久久精品国产亚洲AV天美18 | 国产一卡2卡3卡四卡精品网站 | 日日草视频 | 国产精品麻豆久久99 | 亚洲 小说 欧美 激情 另类 | 免费永久观看美女视频网站网址 | 国产99久久九九精品免费 | 欧美一区二区三区四区在线观看 | 太粗 好紧 使劲舒服 | 四虎永久免费地址 | h动态图男女啪啪27报 | 成人综合婷婷国产精品久久免费 | 成人榴莲视频 | 天堂俺去俺来也www久久婷婷 | 午夜熟女插插XX免费视频 |