赞
踩
使用 ApplicationListener
监听 ContextRefreshedEvent 或 ApplicationReadyEvent 等应用上下文初始化完成事件。
@Component public class CacheWarmer implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { // 执行缓存预热业务... cacheManager.put("key", dataList); } } @Component public class CacheWarmer implements ApplicationListener<ApplicationReadyEvent> { @Override public void onApplicationEvent(ApplicationReadyEvent event) { // 执行缓存预热业务... cacheManager.put("key", dataList); } }
@Component
public class CachePreloader {
@Autowired
private YourCacheManager cacheManager;
@PostConstruct
public void preloadCache() {
// 执行缓存预热业务...
cacheManager.put("key", dataList);
}
}
@Component public class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { // 执行缓存预热业务... cacheManager.put("key", dataList); } } @Component public class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { // 执行缓存预热业务... cacheManager.put("key", dataList); } }
@Component
public class CachePreloader implements InitializingBean {
@Autowired
private YourCacheManager cacheManager;
@Override
public void afterPropertiesSet() throws Exception {
// 执行缓存预热业务...
cacheManager.put("key", dataList);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。