我用TimerTask做个定时任务,但我用的是SSH框架,也就是里面的一些属性是注入的,但这里我就无法解决
先发下我原代码
package com.common;
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyListener implements ServletContextListener {
private Timer timer = null; public void contextInitialized(ServletContextEvent event) {
timer = new Timer(true);
//设置任务计划,启动和间隔时间
//86400000为1天毫秒数
timer.schedule(new MyTask(), 0, 5000);
} public void contextDestroyed(ServletContextEvent event) {
timer.cancel();
}
}package com.common;import java.util.Date;
import java.util.TimerTask;import com.user.dao.MaxLshDao;
import com.user.service.MaxLshService;
public class MyTask extends TimerTask {
private MaxLshService maxLshService;
public MaxLshService getMaxLshService() {
return maxLshService;
}
public void setMaxLshService(MaxLshService maxLshService) {
this.maxLshService = maxLshService;
}
public void run() {
System.out.println("call at " + (new Date()));
/*ShopInUserDao SIUService=new ShopInUserDao();
SIUService.findEndRentDate();*/
}
}再发下web.xml <listener>
<listener-class>com.common.MyListener</listener-class>
</listener>
现在的问题就是TimerTask 里的maxLshService一直都是空的,无法注入 <bean id="myTask" class="com.common.MyTask">
<property name="maxLshService">
<ref bean="maxLshService" />
</property>
</bean>
先发下我原代码
package com.common;
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyListener implements ServletContextListener {
private Timer timer = null; public void contextInitialized(ServletContextEvent event) {
timer = new Timer(true);
//设置任务计划,启动和间隔时间
//86400000为1天毫秒数
timer.schedule(new MyTask(), 0, 5000);
} public void contextDestroyed(ServletContextEvent event) {
timer.cancel();
}
}package com.common;import java.util.Date;
import java.util.TimerTask;import com.user.dao.MaxLshDao;
import com.user.service.MaxLshService;
public class MyTask extends TimerTask {
private MaxLshService maxLshService;
public MaxLshService getMaxLshService() {
return maxLshService;
}
public void setMaxLshService(MaxLshService maxLshService) {
this.maxLshService = maxLshService;
}
public void run() {
System.out.println("call at " + (new Date()));
/*ShopInUserDao SIUService=new ShopInUserDao();
SIUService.findEndRentDate();*/
}
}再发下web.xml <listener>
<listener-class>com.common.MyListener</listener-class>
</listener>
现在的问题就是TimerTask 里的maxLshService一直都是空的,无法注入 <bean id="myTask" class="com.common.MyTask">
<property name="maxLshService">
<ref bean="maxLshService" />
</property>
</bean>
解决方案 »
- java中如何获取ALL USERS \ Application Data目录?
- 谁能给我分析一下这个程序的执行过长 在线等
- android运行的问题
- java 怎么实现根据传入的数据库表的名字将数据生成XML文件
- 很不明白?????????????
- 为什么加了lock还出现问题?
- 我下载了JDK1.4.1_03安装以后JDK目录里没有1.3的Javac文件呀?
- 如何用java得到用户的mac地址
- 请问http://china-programer.myrice.com为什么进不进去?
- ---关于Applet-------
- 在main函数里传进来一个String,往第二个类里传那个赋值于一个String变量,在第三个类里头再使用那个String变量。怎么写?
- 新手求教:读取文件时,输出的中文是乱码,代码如下:求问各位高手问题出在哪里啊?该怎么改正?
这个怎么配的?其他的我看了都没有任何问题自己在看一下,代码没什么问题的。配置问题祝楼主好运
<property name="maxLshDao">
<ref bean="maxLshDao" />
</property>
</bean>这个是没问题的,我有好几个ACTION类,action类都能成功注入
//spring配置文件里的内容 <!--任务到期短信提示-->
<bean id="testService" class="com.fbty.wms.utils.TestService">
<property name="messageService" ref="messageService" />
<property name="wmsJdbcDao" ref="wmsJdbcDao" />
</bean>
<!--注入-->
<bean id="testJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="testService" />
<property name="targetMethod" value="doCheck"></property>
</bean><!-- 触发器 -->
<bean id="testThread"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="testJob" />
<property name="cronExpression" value="0 0 0 * * ? " />
</bean>
<!-- 定时器工厂 -->
<bean
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="testThread"></ref>
</list>
</property>
</bean>
//这个就是TestService里的doCheck方法 /**
* Description: 到期警示告警短信自动扫描<br>
* @throws ServiceException
* @throws RemoteException
* @throws MalformedURLException
*/
public void doCheck() throws MalformedURLException, RemoteException, ServiceException {
System.out.println("开始计时,每隔一分钟提示一次……");
//里面内容就省略了,整个流程重点是配置文件。仅做参考。。
}
public void run() {
System.out.println("call at " + (new Date()));
timer.schedule(new MyTask(), 0, 5000);
}
ServletContextListener 实例是不是spring创建的?应该是web服务器或应用服务器初始化创建的。7楼的方法测试了没有啊。