Spring 多線程下注入bean問題詳解
本文介紹了Spring 多線程下注入bean問題詳解,分享給大家,具體如下:
問題
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,通過構(gòu)造傳過去,代碼如下:
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();
解決方案二
通過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);
}
//只適合一個class只被定義一次的bean(也就是說,根據(jù)class不能匹配出多個該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類
<bean class = "cn.com.infcn.applicationcontext.ApplicationContextHolder"></bean>
根據(jù) bean 的名稱獲取實(shí)例
UserService user = (UserService) ApplicationContextHolder.getBeanByName("userService");
根據(jù) bean 的Class 獲取實(shí)例(如果該Class存在多個實(shí)例,會報錯的)
UserService user = (UserService) ApplicationContextHolder.getBeanByType(UserService.class);
這種方式,不管是否多線程,還是普通的不收spring管理的類,都可以使用該方法獲得spring管理的bean。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Druid連接池未關(guān)閉導(dǎo)致內(nèi)存泄漏問題
這篇文章主要介紹了Druid連接池未關(guān)閉導(dǎo)致內(nèi)存泄漏問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
SpringDataJPA實(shí)體類關(guān)系映射配置方式
這篇文章主要介紹了SpringDataJPA實(shí)體類關(guān)系映射配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Java中ResponseBodyEmitter的實(shí)現(xiàn)
這篇文章主要介紹了Java中ResponseBodyEmitter的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
spring-boot中使用spring-boot-devtools的實(shí)現(xiàn)代碼
這篇文章主要介紹了spring-boot中使用spring-boot-devtools的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Spring?Boot?Actuator?漏洞利用小結(jié)
spring對應(yīng)兩個版本,分別是Spring Boot 2.x和Spring Boot 1.x,因此后面漏洞利用的payload也會有所不同,這篇文章主要介紹了Spring?Boot?Actuator?漏洞利用小結(jié),需要的朋友可以參考下2023-11-11
SpringBoot概述及在idea中創(chuàng)建方式
SpringBoot提供了一種快速使用Spring的方式,基于約定大于配置的思想,可以讓開發(fā)人員不必在配置與邏輯業(yè)務(wù)之間進(jìn)行思維的切換,這篇文章主要介紹了SpringBoot概述及在idea中創(chuàng)建方式,需要的朋友可以參考下2022-09-09

