首先在配置文件头部的必须要有:
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
1、Spring注解配置<task:annotation-driven/>
例子:
spring.xml配置
<context:component-scan base-package="com.hesvit" />
<task:annotation-driven/>
执行任务的bean
@Component
@Lazy(value=false)
public class TaskTest
{
@Scheduled(cron=" 0/5 * * * * ?")
public void testfun() {
System.out.println("每5s执行一次---------飞呀飞呀,我的骄傲放纵");
}
}
2、Spring配置文件配置
例子:
spring.xml配置
<context:component-scan base-package="com.hesvit" />
<task:scheduled-tasks>
<task:scheduled ref="taskTest" method="testfun" cron="0/3 * * * * ?"/>
</task:scheduled-tasks>
执行任务的bean
@Component
@Lazy(value=false)
public class TaskTest
{
public void testfun() {
System.out.println("每5s执行一次---------飞呀飞呀,我的骄傲放纵");
}
}