本来转载于http://kingliu.iteye.com/blog/992325
1.实现SmartLifecycle接口
public class InitRedisCache implements SmartLifecycle{
@Override
public void start() {
// TODO Auto-generated method stub
}
@Override
public void stop() {
// TODO Auto-generated method stub
}
@Override
public boolean isRunning() {
// TODO Auto-generated method stub
return false;
}
@Override
public int getPhase() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isAutoStartup() {
// TODO Auto-generated method stub
return true;
}
@Override
public void stop(Runnable callback) {
// TODO Auto-generated method stub
}
}
2.实现ApplicationListener接口,定义个event,判断event是否相等。
@Lazy(false)
@Repository
public class InitRedisCache implements ApplicationListener{
@Autowired
@Qualifier("dictionaryService")
private DictionaryService dictionaryService;
@Autowired
@Qualifier("redisService")
private RedisService redisService;
@Override
public void onApplicationEvent(ApplicationEvent event) {
if(event instanceof InitEvent){
initDictionaryData();
}
}
public void initDictionaryData(){
}
}
本文介绍了一种在SpringBoot应用中通过实现SmartLifecycle和ApplicationListener接口来管理Redis缓存初始化的方法。通过监听特定事件并进行缓存预热,确保了应用启动时即拥有可用的缓存数据。
518

被折叠的 条评论
为什么被折叠?



