在Spring项目中,有时须要新开线程完成一些复杂任务,而线程中可能须要注入一些服务。而经过Spring注入来管理和使用服务是较为合理的方式。可是若直接在Thread子类中经过注解方式注入Bean是无效的。java
由于Spring自己默认Bean为单例模式构建,同时是非线程安全的,所以禁止了在Thread子类中的注入行为,所以在Thread中直接注入的bean是null的,会发生空指针错误。安全
如下分别列举错误的注入方法和两种解决方式。app
@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。ide
我的比较推荐这种方法,对外部代码的影响较小。函数
@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。this
即在能够注入的地方先获得可用的实例,在经过Thread子类的构造函数引入。这样会使得在进行代码修改时,影响到每一个使用Thread子类的代码,修改工做量大。spa
@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. } }