Pig-man的gravatar头像
Pig-man 2016-03-15 17:03:01
Spring MVC开发中定时器继承Task后无法对service注入问题

最近在做一个Spring+MyBatis的一个项目,其中用到了Redis的存储,然后遇到问题是这样的:

RedisTask是通过定时器来每分钟像数据库里推送的,于是就有了

public class RedisTask  extends Task {

    public void execute(TaskExecutionContext executor) throws RuntimeException {
        Scheduler scheduler = executor.getScheduler();
        TaskExecutor[] executors = scheduler.getExecutingTasks();
        if(executors!=null && executors.length>1)//调度未执行完时不启用新任务

然后在定时器的任务中通过对Redis的调用,推送到本地数据库

OrderService orderService = new OrderService();
Order order = new Order();
int num = orderService.add(order);
System.out.println("插入条数为:" + num);

那么问题来了,tomcat开始没次调用定时器的RedisTask就开始报错,debug发现orderService为null,也就是说service根本就没有注入。

然后我就在Spring的配置文件里查看标注问题,发现根本没问题,再然后发现此方法继承了Task,于是乎恍然大悟

:定时器继承了Task之后,在定时器调用此方法后会直接执行execute方法,来不急执行标注进行注入。

  @Resource
    private OrderService orderService;

然后解决方案:

1、引入ApplicationContextUtil的工具类


package com.aneop.common.util;



import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;



public class ApplicationContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;



    public static ApplicationContext getApplicationContext() {

        return applicationContext;

    }



    public void setApplicationContext(ApplicationContext applicationContext) {

        ApplicationContextUtil.applicationContext = applicationContext;

    }



    public static Object getBean(String beanName) {

        return applicationContext.getBean(beanName);

    }

}

 2、在execute方法中以这种方式进行对service进行注入

OrderService orderService = (OrderService)ApplicationContextUtil.getBean("orderService");

注:在service的接口的@Service标注中必须写明注入的名字。

@Service("orderService")
public class OrderServiceImpl implements OrderService {

 

这样问题就完美收官了,今天晚上把redis的使用新的写一个博客。ok,坐等下班了


打赏
最近浏览
水光浮藻  LV6 2021年4月26日
wjl19890214  LV1 2019年10月24日
Ecocar  LV2 2019年7月19日
ken448491164  LV1 2018年1月23日
873726197  LV1 2017年12月28日
qq531678894 2017年11月22日
暂无贡献等级
weienqing  LV17 2017年11月15日
632443784 2017年8月24日
暂无贡献等级
412940226  LV10 2017年5月21日
红蓝死忠  LV1 2017年3月10日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友