Spring在Thread中注入Bean无效的解决方式
在Spring项目中,有时需要新开线程完成一些复杂任务,而线程中可能需要注入一些服务。而通过Spring注入来管理和使用服务是较为合理的方式。但是若直接在Thread子类中通过注解方式注入Bean是无效的。
因为Spring本身默认Bean为单例模式构建,同时是非线程安全的,因此禁止了在Thread子类中的注入行为,因此在Thread中直接注入的bean是null的,会发生空指针错误。
以下分别列举错误的注入方法和两种解决方式。
错误的注入方法
@Controller
public class SomeController{
@ResponseBody
@RequestMapping("test")
String testInjection(){
// 直接创建并运行线程
new SomeThread().start();
}
}
// 直接编写线程
public SomeThread extends Thread {
@Autowired
SomeService someService;
@Override
public void run(){
// do something...
someService.doSomething();
// 此时 someService实例是null.
}
}
报NullpointException。
通过封装Thread子类注入
个人比较推荐这种方法,对外部代码的影响较小。
@Controller
public class SomeController{
// 通过注解注入封装线程的Bean
@AutoWired
SomeThread someThread;
@ResponseBody
@RequestMapping("test")
String testInjection(){
// 通过注入的Bean启动线程
someThread.execute();
}
}
@Component
public class SomeThread {
// 封装Bean中注入服务
@AutoWired
SomeService someService
public void execute() {
new Worker().start();
}
// 线程内部类,Thread或者Runnable均可
private class Worker extends Thread {
@Override
public void run() {
// do something...
SomeThread.this.someService.doSomething();
// 此时someService已被注入,非null.
}
}
}
正常调用someService。
通过外部引入
即在可以注入的地方先得到可用的实例,在通过Thread子类的构造函数引入。这样会使得在进行代码修改时,影响到每个使用Thread子类的代码,修改工作量大。
@Controller
public class SomeController{
// 通过注解注入Service
@AutoWired
SomeService someService;
@ResponseBody
@RequestMapping("test")
String testInjection(){
// 通过构造函数从外部引入
new Worker(someService).start();
}
}
public class SomeThread {
private SomeService someService;
public SomeThread(SomeService someService){
// 通过构造函数从外部引入
this.someService = someService;
}
@Override
public void run() {
// do something...
someService.doSomething();
// 此时someService非null.
}
}
Spring在Thread中注入Bean无效的解决方式的更多相关文章
- spring给容器中注入组件的几种方式
目录 环境搭建 spring给容器中注入组件 1.包扫描+组件标注注解(@Controller/@Service/@Repository/@Component)适用于把自己写的类加入组件(默认ID类名 ...
- Spring IOC容器中注入bean
一.基于schema格式的注入 1.基本的注入方式 (属性注入方式) 根据setXxx()方法进行依赖注入,Spring只会检查是否有setter方法,是否有对应的属性不做要求 <bean id ...
- Spring在代码中获取bean的几种方式(转:http://www.dexcoder.com/selfly/article/326)
方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类ApplicationObj ...
- Spring为IOC容器注入Bean的五种方式
一 @Import导入组件,id默认是组件的全类名 //类中组件统一设置.满足当前条件,这个类中配置的所有bean注册才能生效: @Conditional({WindowsCondition.clas ...
- Spring在代码中获取bean的几种方式
方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类ApplicationObj ...
- Spring在代码中获取bean的几种方式(转)
获取spring中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象 ApplicationContext ac = new FileSystemXmlApplica ...
- spring 在容器中一个bean依赖另一个bean 需要通过ref方式注入进去 通过构造器 或property
spring 在容器中一个bean依赖另一个bean 需要通过ref方式注入进去 通过构造器 或property
- spring 注入bean的两种方式
我们都知道,使用spring框架时,不用再使用new来实例化对象了,直接可以通过spring容器来注入即可. 而注入bean有两种方式: 一种是通过XML来配置的,分别有属性注入.构造函数注入和工厂方 ...
- Spring的DI(Ioc) - 注入bean 和 基本数据类型
注入bean有两种方式: 注入其他bean: 方式一 <bean id="orderDao" class="cn.itcast.service.OrderDaoBe ...
随机推荐
- spring cloud gateway:Unable to find GatewayFilterFactory with name Hystrix
在springcloud gateway中引用Hystrix filter 编译启动时提示 Unable to find GatewayFilterFactory with name Hystrix ...
- IO调度 | Linux块设备中的IO路径及调度策略
当文件系统通过submit_bio提交IO之后,请求就进入了通用块层.通用块层会对IO进行一些预处理的动作,其目的是为了保证请求能够更加合理的发送到底层的磁盘设备,尽量保证性能最佳.这里面比较重要的就 ...
- SQL-W3School-高级:SQL UNION 和 UNION ALL 操作符
ylbtech-SQL-W3School-高级:SQL UNION 和 UNION ALL 操作符 1.返回顶部 1. SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT ...
- Linux云服务器磁盘不见了?解决方案在这里,云服务器磁盘挂载
用过诸多种云以后,发现有个通病,就是新买的数据盘在机器中找不到.本篇总结一下此类问题的解决方法,望各位点赞,有问题评论区见 一.云服务和物理机一样,你买了云服务器的数据盘以后,就相当于把数据盘直接安装 ...
- shell练习题集合
1. 获取ip或MAC地址(方法不唯一) [root@cicd ~]# ip a| grep 'inet' | awk -F " +" '{print $3}'| awk -F & ...
- Delphi 中自定义异常及异常处理的一般方法
delphi中异常定义如下: TCustomException = class(Exception) private public constructor ...
- lexicalized Parsing
$q$(S $\rightarrow$ NP VP) * $q$(NP $\rightarrow$ NNP) * $q$(VP $\rightarrow$ VB NP) * $q$(NP $\righ ...
- mongodb中对数组的操作命令
mongodb中对数组的操作命令有$push.$ne.$addtoset.$pop.$pull ###addtoset会碰到的问题addtoset解释: 往数组里面加入数据,如果数组里已经存在,则不会 ...
- 【ABAP系列】SAP ABAP 高级业务应用程序编程(ABAP)
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 高级业务应用程 ...
- Introduction - Welcome
摘要: 本文是吴恩达 (Andrew Ng)老师<机器学习>课程,第一章<绪论:初识机器学习>中第1课时<欢迎参加机器学习课程>的视频原文字幕.为本人在视频学习过程 ...