一、创建web项目
项目名称:springtaskdemo1
二、添加支持
1.在lib包添加spring支持
commons-logging.jar
slf4j-api-1.6.1.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-context-support-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
spring-web-3.2.0.RELEASE.jar
三、添加配置文件
1.在项目中创建conf目录
/conf
2.在conf目录下创建配置文件
配置文件名称:applicationContext.xml
配置文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
</beans>
四、修改web.xml文件,添加如下配置
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
五、创建任务类
1.在src下创建包
包名:cn.jbit.springtaskdemo1.job
2.在包创建任务类
类名:SpringTaskJob.java
类内容:
public class SpringTaskJob {
public void run(){
System.out.println("run");
}
}
六、配置任务类
<bean id="taskjob" class="cn.jbit.springtaskdemo1.job.SpringTaskJob"></bean>
<task:scheduled-tasks>
<task:scheduled ref="taskjob" method="run" cron="0 17 10 * * ?"/>
</task:scheduled-tasks>
七、部署并启动服务
转载于:https://blog.51cto.com/suyanzhu/1566583