使用@Schedule + corn 表达式
每隔一段时间执行一次
@Scheduled(cron = "0 0/2 * * * ?")
public void runScheduleTask(){
}
@SpringBootApplication
@EnableScheduling
public class xxxxxApplication {
public static void main(String[] args) {
SpringApplication.run(xxxxxApplication.class, args);
}
}
CommandLineRunner实现这个类,重写run方法
/**
* @Classname RoleAndMenuCache
* @Description Springboot 启动之后会自动执行
* @Date 2022/7/1 20:21
* @Created by 连磊
*/
@Component
public class RoleAndMenuCache implements CommandLineRunner {
public static Logger logger = LoggerFactory.getLogger(RoleAndMenuCache.class);
public static List<Role> roleList;
@Autowired
private RoleService roleService;
@Autowired
private RoleService menuService;
@Override
public void run(String... args) throws Exception {
roleList = roleService.loadAll();
logger.info("角色信息缓存完成");
}
}