先在springboot启动类添加注解@EnableScheduling
@SpringBootApplication
@MapperScan("com.example.demo.dao")//dao扫描路径
@EnableScheduling//这里添加
public class SpringBootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisApplication.class, args);
}
}
然后在拦截内容类添加@Component在方法前加时间表达式@Scheduled(fixedRate=20000)
@Component
public class TestJob {
public final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private UserDao userDao;
@Scheduled(fixedRate=20000)
public void testTasks(){
System.out.println(userDao.getUserAll());
}
@Scheduled(cron="0 0/1 * * * ?") //每分钟执行一次,这是cron表达式
public void statusCheck() {
logger.info("每分钟执行一次。");
}
@Scheduled(fixedRate=20000)
public void testTasks() {
logger.info("每20秒执行一次。");
}
}