问题

Spring中多线程注入userThreadService注不进去,显示userThreadService为null异常
代码如下:

public class UserThreadTask implements Runnable {@Autowiredprivate UserThreadService userThreadService;@Overridepublic void run() {AdeUser user = userThreadService.get("0");System.out.println(user);}
}

解决方案一

把要注入的Service,通过构造传过去,代码如下:

public class UserThreadTask implements Runnable {private UserThreadService userThreadService;public UserThreadTask(UserThreadService userThreadService) {this.userThreadService = userThreadService;}@Overridepublic 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;@Overridepublic 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类

<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。


想了解更多精彩内容请关注我的公众号

本人简书blog地址:http://www.jianshu.com/u/1f0067e24ff8    
点击这里快速进入简书

GIT地址:http://git.oschina.net/brucekankan/
点击这里快速进入GIT

Spring 多线程下注入bean问题相关推荐

  1. Spring——Filter过滤器注入Bean时注入失败[NULL]

    问题描述 Spring中Filter注入Bean时注入失败,Bean一直为空. @Slf4j @Component public class RestAuthFilter extends FormAu ...

  2. Autowired,Qualifier,Spring 按名称注入bean属性

    Autowired,Qualifier,Spring 按名称注入bean属性 @Autowired@Qualifier("addItemDestination")private T ...

  3. spring注解方式注入bean

    用注解的方式注入bean,spring的配置文件也要增加一些约束和导入注解所在的包 applicationContext.xml 1 <?xml version="1.0" ...

  4. Spring中注解注入bean和配置文件注入bean

    注解的方式确实比手动写xml文件注入要方便快捷很多,省去了很多不必要的时间去写xml文件 按以往要注入bean的时候,需要去配置一个xml,当然也可以直接扫描包体,用xml注入bean有以下方法: 1 ...

  5. Spring 使用@Import注入Bean的三种方式

    一.准备工作 1.导入spring-context依赖 <dependency><groupId>org.springframework</groupId>< ...

  6. 【Spring学习】spring注解自动注入bean

    Spring mvc注解用到的配置: <!-- 启用spring mvc 注解 --> <context:annotation-config /> <context:co ...

  7. 解决springboot使用多线程无法注入Bean问题,不能注入Service或者Mapper

    工具类 package com.ph.rfwgzw.utils;import org.springframework.beans.BeansException; import org.springfr ...

  8. 1.spring:helloword/注入/CDATA使用/其他Bean/null级联/p命名空间

    新建工程,导入jar,添加spring配置文件(配置文件xxxx.xml)! 1.Helloword实现 Helloword.java public class HelloWord {private ...

  9. Spring的依赖注入和管理Bean

    采用Spring管理Bean和依赖注入 1.实例化spring容器 和 从容器获取Bean对象 实例化Spring容器常用的两种方式: 方法一: 在类路径下寻找配置文件来实例化容器 [推荐使用] Ap ...

最新文章

  1. 顺F速运,你被爱加M坑了
  2. linux 镜像自动安装,制作能自动安装的CentOS镜像文件
  3. new JSONArray(ListMap).tostring()问题
  4. spark sql uv_使用Spark Streaming SQL进行PV/UV统计
  5. 字符动图_图解redis五种数据结构底层实现(动图哦)
  6. 可视化model 参数
  7. 四川大学计算机学院琚生根教授,基于卷积神经网络和自注意力机制的文本分类模型...
  8. git已经删除了远程分支,但本地环境仍能看到远程分支
  9. 20172316 2018-2019-1 《程序设计与数据结构》实验二报告
  10. SylixOS 缺页异常
  11. Linux shell 查找操作
  12. java私塾初级_Java私塾初级教程-19
  13. 2016matlab打开toolbox,MATLAB2016添加工具箱toolbox方法,有截图
  14. 零基础如何学习云计算?云计算学习教程
  15. 破解三大安防视频痛点,看华为4大硬核视频上云技术
  16. 使用bable打包js
  17. 2018年度总结(人若无名,专心练剑)
  18. 这4个文档排版方式掌握了,工作效率提高的不止一点点!
  19. 【转载】SpringBoot 接口数据加解密技巧,so easy!
  20. 计算机网络和因特网笔记

热门文章

  1. 大连海事学院计算机研究生多少分,大连海事大学计算机或软件工程研究生多少分能录取...
  2. 送书福利 | 哈工大SCIR倾力打造NLP新书,详解预训练语言模型
  3. ACL 2019 开源论文 | 使用跨领域语言建模的跨领域命名实体识别
  4. ICDAR 2019国际竞赛召开在即,百度联合学界重奖破局者!
  5. 招聘 | 南京柯基数据招聘自然语言处理工程师
  6. Tree-CNN:一招解决深度学习中的「灾难性遗忘」
  7. Module not found: Error: Can‘t resolve ‘sass-loader‘ in E:\IdeaProject\xinguan\xinguan
  8. Java与ElasticSerach的整合
  9. android 同步执行方法吗,Android异步方法以同步方式实现
  10. python创建float型的列表_如何在Python中进行列表的创建?