好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

解决Spring在Thread中注入Bean无效的问题

在Thread中注入Bean无效

在Spring项目中,有时需要新开线程完成一些复杂任务,而线程中可能需要注入一些服务。而通过Spring注入来管理和使用服务是较为合理的方式。但是若直接在Thread子类中通过注解方式注入Bean是无效的。

因为Spring本身默认Bean为单例模式构建,同时是非线程安全的,因此禁止了在Thread子类中的注入行为,因此在Thread中直接注入的bean是null的,会发生空指针错误。

以下分别列举错误的注入方法和两种解决方式。

错误的注入方法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

@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子类注入

个人比较推荐这种方法,对外部代码的影响较小。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

@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子类的代码,修改工作量大。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

@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多线程中bean的注入问题

最近碰到了一个问题,使用SSM框架,在Service层需要另开一个线程,这个线程专门用来做一些操作,并将结果写入数据库中。但是在线程中使用@Resource或者@Autowired注入全部为NULL,原来是Spring不能在线程中注入。

网上的主要解决方法有

将需要的Bean作为线程的的构造函数的参数传入 使用ApplicationContext.getBean方法来静态的获取Bean

我的线程中所需要的Bean的数量较多,并且以后还有可能增加或者减少,所以方法1并不适合

我的Spring配置文件并不只一个,而且使用getBean方法需要重新加载一遍所有的Bean,这样也违反的Spring的IoC,并不是我想要的,所以也不采用方法2

最后确定使用内部类的方法,将线程中需要的Bean提前注入好,大致的结构如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

@Service

class TestExample{

     //这两个为线程所需要的Bean

     @Resource

     TestDao testDao;

     @Resource

     NeedDap needDao;

     public void serviceExecute(){

         //在这里开启线程,执行操作

         ThreadExample te = new ThreadExample();

         te.start();

     }

    

     //内部类

     private class ThreadExample extends Thread{

         public ThreadExample(){

             //也可以在构造函数中传入参数

         }

         public void run(){

             //这里为线程的操作

             //就可以使用注入之后Bean了

         }

     }

}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/tzdwsy/article/details/53911476

查看更多关于解决Spring在Thread中注入Bean无效的问题的详细内容...

  阅读:17次