首先在applicationContext.xml中增加 

文件头中增加一条 

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
<beans xmlns:task="http://www.springframework.org/schema/task"
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd" >

<!-- 定义使用注解自动扫描的包 -->
<context:component-scan base-package="xxx" /> 定时器必须属于扫描的包中

<!-- 打开定时器开关 -->

<task:annotation-driven/>

下面是定时器的写法

fixedDelay = 5000 表示每隔5秒执行
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component  
public class TestJob {
    @Scheduled(fixedDelay = 5000) 
    public void test()
    {
        System.out.println("job 开始执行"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    } 
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-15
  • 2021-09-21
  • 2021-11-02
  • 2021-06-01
  • 2022-01-14
  • 2021-07-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-07
  • 2022-02-09
  • 2021-04-17
  • 2022-12-23
  • 2022-02-09
相关资源
相似解决方案