1、引入Spring相关jar包
2、定时任务类
package taskJob;
public class TaskDemo {
public void test(){
System.out.println("现在是北京时间:XXX,开始触发定时任务...");
}
}
3、spring-task-context.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:util="http://www.springframework.org/schema/util"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
注意这里:=========
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
注意这里:=========
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
">
<!-- 执行定时任务的类 -->
<bean id="firsttask" class="taskJob.TaskDemo">
</bean>
<!-- 配置任务线性池 -->
<task:executor id="executor" pool-size="3" />
<task:scheduler id="scheduler" pool-size="3" />
<!-- 启用annotation方式 -->
<task:annotation-driven scheduler="scheduler"
executor="executor" proxy-target-class="true" />
<!-- ref:引入执行定时任务的类 method:执行定时任务的方法 -->
<task:scheduled-tasks scheduler="scheduler">
<task:scheduled ref="firsttask" method="test"
cron="0 42 15 * * ?" />
</task:scheduled-tasks>
</beans>
4、配置web.xml,实现项目启动自动执行定时任务
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/spring-task-context.xml
</param-value>
</context-param>
你要做的事情就是自己成功的速度快于父母老去的速度