本篇文章給大家分享的是有關(guān)利用Spring 怎么將bean注入多線程,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
成都創(chuàng)新互聯(lián)公司是一家專(zhuān)業(yè)從事成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)的品牌網(wǎng)絡(luò)公司。如今是成都地區(qū)具影響力的網(wǎng)站設(shè)計(jì)公司,作為專(zhuān)業(yè)的成都網(wǎng)站建設(shè)公司,成都創(chuàng)新互聯(lián)公司依托強(qiáng)大的技術(shù)實(shí)力、以及多年的網(wǎng)站運(yùn)營(yíng)經(jīng)驗(yàn),為您提供專(zhuān)業(yè)的成都網(wǎng)站建設(shè)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)及網(wǎng)站設(shè)計(jì)開(kāi)發(fā)服務(wù)!
問(wèn)題
Spring中多線程注入userThreadService注不進(jìn)去,顯示userThreadService為null異常
代碼如下:
public class UserThreadTask implements Runnable { @Autowired private UserThreadService userThreadService; @Override public void run() { AdeUser user = userThreadService.get("0"); System.out.println(user); } }
解決方案一
把要注入的Service,通過(guò)構(gòu)造傳過(guò)去,代碼如下:
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); } }
Thread t = new Thread(new UserThreadTask(userThreadService)); t.start();
解決方案二
通過(guò)ApplicationContext中獲取需要使用的Service
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; } //根據(jù)bean name 獲取實(shí)例 public static Object getBeanByName(String beanName) { if (beanName == null || context == null) { return null; } return context.getBean(beanName); } //只適合一個(gè)class只被定義一次的bean(也就是說(shuō),根據(jù)class不能匹配出多個(gè)該class的實(shí)例) 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類(lèi)
<bean class = "cn.com.infcn.applicationcontext.ApplicationContextHolder"></bean>
根據(jù) bean 的名稱(chēng)獲取實(shí)例
根據(jù) bean 的Class 獲取實(shí)例(如果該Class存在多個(gè)實(shí)例,會(huì)報(bào)錯(cuò)的)
這種方式,不管是否多線程,還是普通的不收spring管理的類(lèi),都可以使用該方法獲得spring管理的bean。
以上就是利用Spring 怎么將bean注入多線程,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文題目:利用Spring怎么將bean注入多線程
當(dāng)前鏈接:http://sd-ha.com/article12/jsjedc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、建站公司、外貿(mào)網(wǎng)站建設(shè)、商城網(wǎng)站、面包屑導(dǎo)航、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)