spring注解 实现定时任务

来源:互联网 发布:sev18软件下载 编辑:程序博客网 时间:2024/06/04 18:09

一、配置spring.xml文件

      

 1、在beans加入xmlns:task="http://www.springframework.org/schema/task"

在xsi:schemaLocation中加入

http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd



2、task任务扫描注解<task:annotation-driven />

3、配置扫描的位置 

<context:component-scan base-package="com.test"/>  

二、java代码实现

@Component  // 实现定时任务的类必须被@Component注解2 public class Test {3 4     @Scheduled(fixedDelay = 5000)5     public void test() {  // 定时器的任务方法不能有返回值6         System.out.println("我是定时任务,每5秒钟执行一次!");7     }8 }

 

原创粉丝点击