spring的定时任务配置分为三个步骤:
1、定义任务
2、任务执行策略配置
3、启动任务
cron表达式:可以灵活地定义一个计划任务
方式一、配置xml——定义任务并配置策略
<!--配置定时任务-->
<bean id="dd" class="com.obcy.ddd"></bean>
<task:scheduled-tasks>
<!--ref:哪个类,method:哪个方法-->
<task:scheduled ref="dd" method="test" cron="*/3 * * * * ?"/>
</task:scheduled-tasks>
方式二、贴注解——定义任务并配置策略
先在类上贴注解把类放入ioc:Component
在在方法上贴scheduled注解,这个注解有一个cron属性,值是cron表达式
三,启动。任何一种方法配置好了之后,只需要启动spring就行了。
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}