edisTask是通过定时器来每分钟像数据库里推送的,于是就有了
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的工具类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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 {