国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - JAVA教程 - spring如何動態(tài)指定具體實現(xiàn)類

spring如何動態(tài)指定具體實現(xiàn)類

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

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

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

一.接口與實現(xiàn)類:

?
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();
}
 
// 具體兩個實現(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";
  }
 
}

在實現(xiàn)類中重寫了toString() 方法,可以自定義字符串,當調(diào)用時傳入指定的字符串就能獲取到相應(yīng)的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;
  }
 
  // 獲取接口實現(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ù)傳入service.toString()的具體字符串就能獲取到相應(yīng)的bean
    // 此處也可以不做以下的操作,直接使用beanMap,在調(diào)用時,傳入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();
}

運行結(jié)果,如圖:

spring如何動態(tài)指定具體實現(xiàn)類

備注:

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

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

?
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;
  }
}

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

注意:

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

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

 

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲一区 | 日本欧美久久久久免费播放网 | 韩国精品一区二区 | 亚洲午夜剧场 | 天天操一操 | 91精品国产91久久久久久吃药 | 自拍偷拍视频网站 | av福利在线观看 | 国产乱码精品一区二区三区av | 在线一级毛片 | 亚洲免费a | 一区二区视频 | 99精品久久久久久久免费 | 精品久久久久久久久久 | 亚洲精品免费看 | 五月天综合网 | 黄色片网站在线看 | 在线观看成人 | 国产一级高清视频 | 一区二区三区不卡视频 | 亚洲一区二区在线播放 | 五月婷婷丁香在线 | 蜜桃视频网站入口 | 欧美成人免费视频 | 久草社区 | 91精品久久久久久综合五月天 | 亚洲精品午夜 | 欧洲亚洲精品久久久久 | 欧美色综合网 | 欧美精品一区二区三区蜜桃视频 | 久久精品电影 | 免费视频一区 | 黄色最新网站 | 亚洲国产高清在线 | 精品麻豆剧传媒av国产九九九 | 亚洲精品在线视频观看 | 黄色影院在线观看 | 成年人免费网站 | 激情五月婷婷综合 | 91久久国产露脸精品国产护士 | 天堂av中文字幕 |