// 每两秒执行一次
@Scheduled(cron = “*/2 * * * * ?”)
public void sayHello(){
System.out.println(“Hello, menmen!”+Thread.currentThread().getName());
}
}
复制代码
基于SchedulingConfigurer接口
import com.example.demojpa.dao.CronRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
/**
-
@author: zp
-
@Date: 2019-09-28 17:33
-
@Description:
*/
@Configuration
@EnableScheduling
public class TaskBasedInterface implements SchedulingConfigurer {
/**
-
这是JPA,Mapper可以注入Mapper文件
-
都是为了从数据库读取配置 *3 * * * * ?
*/
@Autowired
CronRepository cronRepository;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addCr