Java(多)线程中注入Spring的Bean

问题说明
今天在web应用中用到了Java多线程的技术来并发处理一些业务,但在执行时一直会报NullPointerException的错误,问题定位了一下发现是线程中的Spring bean没有被注入,bean对象的值为null。
 
原因分析
web容器在启动应用时,并没有提前将线程中的bean注入(在线程启动前,web容器也是无法感知的)
 
解决方案
方法有多种,网上也看到了不少。
1. 使用static声明变量
http://blog.csdn.net/bjamosgavin/article/details/6125497
这个方法自己试了一下但是没有成功。。。
2. 把线程设置为主程序的内部类
这也是一种简单的方法,主程序在web容器加载时肯定是可以注入Spring bean的,那么将线程的实现类放在主程序的类中便可以“共享”Spring的bean,(当然,这需要提前把线程中的需要用到的bean定义在外层的类中)。
具体操作方法,就是将生成线程的线程池定义在主程序的类中,每个线程的实现类作为内部类也定义在主程序中。这个方法自己试过,是可以的。
3. 使用静态方法直接取的容器中的spring对象
这个方法稍微专业点,可以线程的分发与线程的实现分离出来。在每个线程中使用静态方法直接取的容器中的spring对象。
使用静态方法获取容器中的spring对象可以参见
http://littie1987.iteye.com/blog/937877,
或者http://my.oschina.net/skyline520/blog/181158?p={{page}}
但一定要记住,你定义这个工具类也要配置成spring中的bean!
 
下面贴一下我在使用时的代码
(1)定义工具类
public class SpringApplicationContextHolder implements ApplicationContextAware {
 
    private static ApplicationContext context;
 
    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        SpringApplicationContextHolder.context = context;
    }
 
   
    public static Object getSpringBean(String beanName) {
        notEmpty(beanName, "bean name is required");
        return context==null?null:context.getBean(beanName);
    }
 
    public static String[] getBeanDefinitionNames() {
        return context.getBeanDefinitionNames();
    }
 
(2)在Spring中注册工具类的bean
(3)线程中获取bean
UserRepo user = (UserRepo) SpringApplicationContextHolder.getSpringBean("userRepo");
posted @ 2016-11-24 15:05  费曼带我飞  阅读(1257)  评论(0编辑  收藏  举报