线程中调用service方法出错

问题已经解决,不过还会要记录下。

public class PnFileTGIComputeThread implements Runnable {
    @Resource
    private AppUsedService   appUsedService;
//    AppUsedService appUsedService = (AppUsedService) AllBean.getBean("appUsedService");
    public  String taskId;
    public  int  cityId;
    public PnFileTGIComputeThread(String name, int cityId){
        this.taskId = name;
        this.cityId = cityId;
    }
    @Override
    public void run() {
        try {
            this.appUsedService.doSaveAzTaskAppUsedInfoCity(Integer.valueOf(taskId),cityId);
        } catch (Exception e){
           e.printStackTrace();
        }
    }
}
新建了一个线程,然后再主线程中去实例化本线程,启动线程。DUG问题是,线程启动后,参数也都传过来了,但是通过注解来注入的service一直是null值。

老办法,翻了度娘的牌子,找到问题,在线程中为了线程安全,是防注入。没办法,要用到这个类啊。只能从bean工厂里拿个实例了


public class AllBean implements ApplicationContextAware{
   
    private static ApplicationContext applicationContext; 
   
    public void setApplicationContext(ApplicationContext context) {
       AllBean.applicationContext = context;
       }
   
    public static Object getBean(String name){
         return applicationContext.getBean(name);
    }
    
     public static ApplicationContext getApplicationContext() {  
          return applicationContext;  
      }  
}
getbean方法,获取上下文中的bean, 不过呢要有点问题,这个AllBean类需要在在Bean工厂中注册下

<bean id="allBean" class="xxxxx.AllBean"  />
  想要啥东西,现在都可以直接去getBean,例如:

AppUsedService appUsedService = (AppUsedService) AllBean.getBean("appUsedService");
好的,线程正常启动了。

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值