激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring 多線程下注入bean問題詳解

Spring 多線程下注入bean問題詳解

2021-01-19 11:05jijs Java教程

本篇文章主要介紹了Spring 多線程下注入bean問題詳解,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文介紹了Spring 多線程下注入bean問題詳解,分享給大家,具體如下:

問題

Spring中多線程注入userThreadService注不進去,顯示userThreadService為null異常

代碼如下:

?
1
2
3
4
5
6
7
8
9
10
public class UserThreadTask implements Runnable {
  @Autowired
  private UserThreadService userThreadService;
 
  @Override
  public void run() {
    AdeUser user = userThreadService.get("0");
    System.out.println(user);
  }
}

解決方案一

把要注入的Service,通過構造傳過去,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class UserThreadTask implements Runnable {
  private UserThreadService userThreadService;
 
  public UserThreadTask(UserThreadService userThreadService) {
    this.userThreadService = userThreadService;
  }
 
  @Override
  public void run() {
    AdeUser user = userThreadService.get("0");
    System.out.println(user);
  }
}
?
1
2
Thread t = new Thread(new UserThreadTask(userThreadService));
t.start();

解決方案二

通過ApplicationContext中獲取需要使用的Service

?
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
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
 
public class ApplicationContextHolder implements ApplicationContextAware {
  private static ApplicationContext context;
  @Override
  public void setApplicationContext(ApplicationContext context) throws BeansException {
    ApplicationContextHolder.context = context;
  }
  //根據bean name 獲取實例
  public static Object getBeanByName(String beanName) {
    if (beanName == null || context == null) {
      return null;
    }
    return context.getBean(beanName);
  }
  //只適合一個class只被定義一次的bean(也就是說,根據class不能匹配出多個該class的實例)
  public static Object getBeanByType(Class clazz) {
    if (clazz == null || context == null) {
      return null;
    }
    return context.getBean(clazz);
  }
  public static String[] getBeanDefinitionNames() {
    return context.getBeanDefinitionNames();
  }
}

Spring 加載自己定義的ApplicationContextHolder類

?
1
<bean class = "cn.com.infcn.applicationcontext.ApplicationContextHolder"></bean>

根據 bean 的名稱獲取實例

 

復制代碼 代碼如下:

UserService user = (UserService) ApplicationContextHolder.getBeanByName("userService");

 

 

根據 bean 的Class 獲取實例(如果該Class存在多個實例,會報錯的)

 

復制代碼 代碼如下:

UserService user = (UserService) ApplicationContextHolder.getBeanByType(UserService.class);

 

這種方式,不管是否多線程,還是普通的不收spring管理的類,都可以使用該方法獲得spring管理的bean。

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

原文鏈接:http://www.jianshu.com/p/c8a43d26bc0f?utm_source=tuicool&utm_medium=referral

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产一区网址 | 91 免费视频| 国产精品一区二区三区99 | 精国品产一区二区三区有限公司 | 成人羞羞在线观看网站 | 成人毛片100部 | 免费观看的毛片手机视频 | 国产精品99一区二区 | 性猛交ⅹxxx乱巴西 欧美日韩1区2区3区 | 国产第一页精品 | 99在线热播精品免费 | 成年性羞羞视频免费观看 | 激情97| 欧美国产成人在线 | 精品久久久久久久久中文字幕 | 欧美黄一级 | 成年性羞羞视频免费观看 | 欧美一级电影在线观看 | 成人三级电影网 | 一区二区三区日韩在线 | 午夜看毛片 | av观看国产 | 国产成人精品自拍视频 | 成人免费毛片在线观看 | 一区二区三区日韩在线 | 成人福利电影在线观看 | 一级黄色影片在线观看 | 中国av免费观看 | 在线日韩在线 | 国内精品国产三级国产a久久 | 黄污在线观看 | 在线中文字幕观看 | 国产chinesehd精品91 | 正在播放91| 影视免费观看 | 成年性羞羞视频免费观看 | 综合精品一区 | 一级免费在线视频 | 福利在线国产 | 亚洲一区二区三区四区精品 | 国产精品成aⅴ人片在线观看 |