SpringMVC中定时器继承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,坐等下班了

 

posted @ 2016-03-11 17:08  陽倌_℡__  阅读(1425)  评论(1编辑  收藏  举报