场景是为了减少对数据库的访问,减轻服务器压力,用到定时器定时执行查数据库把数据导出到excel。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@EnableScheduling //开启定时
@Component //声明为spring组件
public class className{
private static final Logger LOGGER= LoggerFactory.getLogger(className.class);
@Scheduled(cron = "0/10 * * * * *") //定时执行时间
public void exportSocialSecurityTimer() {
try{
LOGGER.debug("执行定时的任务");
}
catch (Exception e) {
LOGGER.error("定时任务出现异常!",e);
}
}
}