一、Quartz
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
//配置调度工厂 这个id名可以随便取
<bean id="schedulID" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="areaRecommenderTrigger"/>
</list>
</property>
</bean>
//调用类所在的位置
<bean id="areaRecommenderCount" class="com.aplus.action.AreaRecommenderCountAction">
</bean>
<bean id="areaRecommenderCountJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="areaRecommenderCount"/>
<property name="targetMethod" value="areaData"/>
<property name="concurrent" value="false"/>
</bean>
//定时
<bean id="areaRecommenderTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="areaRecommenderCountJob" />
<property name="cronExpression">
<value>0 0 23 * * ?</value>
</property>
</bean>
</beans>
二、Spring Task
//在spring中加入以下代码:
<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="5" />
<task:annotation-driven executor="executor" scheduler="scheduler" />
- 然后给定时任务类添加@Component、@Scheduled注解(org.springframework.scheduling.annotation.Scheduled),给任务方法添加@Scheduled(cron = “0/5 * * * * ?”)注解,并让Spring扫描到该类。