日期:2014-05-18  浏览次数:20612 次

TimerTask问题
在框架中使用Timer定时任务
<!-- 定时任务 -->
 <listener>
<listener-class>com.webterren.tnews.comm.TimerTask.MyListener</listener-class>
</listener>

得到spring的bean
public void run() {
     ServletContext context = ServletActionContext.getServletContext();
     WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
      Object obj = wac.getBean(beanId);
}
run方法能执行,为什么在获得spring bean的时候就报空指针异常
即便是new 一个action,但是action调用service的时候也报空指针,
想问下这是什么情况,spring要配置什么?
谢谢各位大神了!!!!
------解决方案--------------------
可能web.xml加载顺序的问题,你把监听器的listener 放到web.xml的最后面,试试看!
------解决方案--------------------
  不知道 你 是 怎么想的,,给点代码 你看看

OpmequipmentService opmequipmentService = (OpmequipmentService) WebHelper.getBean("opmequipmentService");
OpmnodeService opmnodeService = (OpmnodeService) WebHelper.getBean("opmnodeService");
AlarmcenterService alarmcenterService = (AlarmcenterService) WebHelper.getBean("alarmcenterService");
try {
//每次开始采集时,将告警表中的数据标识为已修复
boolean b = Work_Day.checkDate(); //验证今天是否为节假日 true为工作日 false为节假日
if(b){
alarmcenterService.updateFlgdeletedAll();
}
opmequipmentService.getOPMEquipmentList("in");
opmnodeService.getOpmnodeList("in");
} catch (Exception e) {

------解决方案--------------------
 ServletContext context = ServletActionContext.getServletContext();问题出在这,你的容器都还没完全启动,这里怎么能用struts的东西来获取上下文呢?