在spring+mybatis 里用timer定时器出错,请帮忙看一下!

石头子 2013-07-23 01:53:11
SQL语句,在查询分析器,通过


<update id="refreshMarketMoney">
update usertable set umoney=umoney+100
</update>


定时类代码:



package com.UserManage.common.timers;

import java.util.Calendar;
import java.util.TimerTask;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import jxproduct.controller.simulatedTrading.*;
import com.UserManage.dao.SqlDao;
public class userTaskCenter extends TimerTask {


@Autowired
private SqlDao userDao;
public void updateUserTimers() {
//下面的SQl无参数,但 定时启动,报 空指针错误
//对此不熟悉,猜可能是userDao,没有初始化
userDao.update("com.UserManage.mapper.refreshMarketMoney");
}
@Override
public void run() {
updateUserTimers();
}
}



applicationContext.xml配置


<bean id="timerTask" class="com.UserManage.common.timers.userTaskCenter"></bean>
<bean id="repeatingtriger" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="delay">
<value>5000</value>
</property>
<property name="period">
<value>3000</value>
</property>
<property name="timerTask">
<ref local="timerTask" />
</property>
</bean>
<bean id="schedule"
class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref local="repeatingtriger" />
</list>
</property>
</bean>



错误:
Exception in thread "schedule" java.lang.NullPointerException
at com.UserManage.common.timers.userTaskCenter.updateAccountTimers(tHandleSimulatedTrading.java:22)
at com.UserManage.common.timers.userTaskCenter.run(tHandleSimulatedTrading.java:30)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)

谢谢各位
...全文
232 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
小丑哥_V5 2013-07-23
  • 打赏
  • 举报
回复
一开始启动就报错吗?如果是这样的话 把event穿进去

public void contextInitialized(ServletContextEvent event) {
		timer = new Timer(true);
		event.getServletContext().log("定时器启动");
		timer.schedule(new OrderTask(event.getServletContext()), 0,
				INTERVAL_TIME);
		event.getServletContext().log("添加到任务调度表");
	}

public OrderTask(ServletContext servletContext) {
		this.orderService = (OrderService) BaseUtilSupport.SpringUtil.getBean(
				servletContext, "orderService");
		params = new HashMap<String, Object>();
		params.put("status", 1);
		this.mobanService = (MobanService) BaseUtilSupport.SpringUtil.getBean(
				servletContext, "mobanService");
		this.smsService = (SmsService) BaseUtilSupport.SpringUtil.getBean(
				servletContext, "smsService");
	}
S117 2013-07-23
  • 打赏
  • 举报
回复
引用 4 楼 mbcls 的回复:
[quote=引用 2 楼 longtian1213 的回复:] 很明显你SqlDao没有注入呗!为什么在applicationContext配置了userTaskCenter却在代码中自动装配呢? 在userTaskCenter 定义一个setSqlDao 方法去掉@Autowired,在applicationContext修改 <beans default-autowire="autodetect">
试过还是报错,SqlDao注入不了~~[/quote] 实在不行就放弃注入吧!直接new一个吧!
石头子 2013-07-23
  • 打赏
  • 举报
回复
userDao打印出来是null
石头子 2013-07-23
  • 打赏
  • 举报
回复
引用 2 楼 longtian1213 的回复:
很明显你SqlDao没有注入呗!为什么在applicationContext配置了userTaskCenter却在代码中自动装配呢? 在userTaskCenter 定义一个setSqlDao 方法去掉@Autowired,在applicationContext修改 <beans default-autowire="autodetect">
试过还是报错,SqlDao注入不了~~
石头子 2013-07-23
  • 打赏
  • 举报
回复
引用 1 楼 ldh911 的回复:
Exception in thread "schedule" java.lang.NullPointerException at com.UserManage.common.timers.userTaskCenter.updateAccountTimers(tHandleSimulatedTrading.java:22) “tHandleSimulatedTrading.java”文件里面的第22行,是哪行? 另外“updateAccountTimers()”这个函数也没看到在哪儿。。。
22行是:userDao.update("com.UserManage.mapper.refreshMarketMoney"); updateAccountTimers就是updateUserTimers
S117 2013-07-23
  • 打赏
  • 举报
回复
很明显你SqlDao没有注入呗!为什么在applicationContext配置了userTaskCenter却在代码中自动装配呢? 在userTaskCenter 定义一个setSqlDao 方法去掉@Autowired,在applicationContext修改 <beans default-autowire="autodetect">
MiceRice 2013-07-23
  • 打赏
  • 举报
回复
Exception in thread "schedule" java.lang.NullPointerException at com.UserManage.common.timers.userTaskCenter.updateAccountTimers(tHandleSimulatedTrading.java:22) “tHandleSimulatedTrading.java”文件里面的第22行,是哪行? 另外“updateAccountTimers()”这个函数也没看到在哪儿。。。

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧