标签:
推荐使用基于配置XML的形式 !!!
//首先要定义schema 空间 <?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:task="http://www.springframe work.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.o rg/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring -task-3.0.xsd"> //先来一种 比较笨重的方法 ...要是多个定时任务 就有点吐啦 <bean id="schedule" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="testTrigger" /> </list> </property> </bean> <bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="testJobDetail" /> <property name="cronExpression" value="0 0/1 * * * ?" /> </bean> <bean id="testJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="targetTestService" /> <!--定时任务的类--> <property name="targetMethod" value="job1" /> <!--要执行的方法--> </bean> <bean id="targetTestService" class="com.tepusoft.web.TaskJob" scope="prototype"> </bean>
再来一个比较简洁的多定时任务的配置(推荐)
<task:annotation-driven /> <!-- 定时器开关,一定要加--> <bean id="taskTest" class="com.tepusoft.web.TaskJob"></bean> <!--定时任务的类--> <task:scheduled-tasks> <!--这里面可以定义多个定时任务--> <task:scheduled ref="taskTest" method="job1" cron="5/3 * * * * ?" /> </task:scheduled-tasks>
基于注解的方式:
        <task:executor id="executor" pool-size="5" />
	<task:scheduler id="scheduler" pool-size="10" />
	<task:annotation-driven executor="executor" scheduler="scheduler" />
        // 定义  POJO 定时任务类 (基于注解的)  一定要spring容器扫描到哟
        /**
         * Spring3 @Scheduled 演示
         * @author GIE 2015年8月10日22:11:17
         */
        @Component
        public class SpringTaskDemo {
	    @Scheduled(cron = "0/5 * * * * *")
	    void doSomethingWith(){
		System.out.println("I‘m doing with cron now!");
	}
    }
标签:
原文地址:http://my.oschina.net/giegie/blog/490378