@WebListener
public class AyUserListener implements ServletContextListener {
@Resource
private RedisTemplate redisTemplate;
@Resource
private AyUserService ayUserService;
private static final String ALL_USER = "ALL_USER_LIST";
@Override
public void contextInitialized(ServletContextEvent sce) {
//查询数据库所有用户
List<AyUser> ayUserList = ayUserService.findAll(); //断点到此处发现成员变量值都是null
//清除缓存中的用户数据
redisTemplate.delete(ALL_USER);
//将数据存放到Redis缓存中
redisTemplate.opsForList().leftPushAll(ALL_USER, ayUserList);
//真实项目中需要注释掉,查询所有的用户数据
List<AyUser> queryUserList = redisTemplate.opsForList().range(ALL_USER, 0, -1);
System.out.println("缓存中目前的用户数有:" + queryUserList.size() + "人");
System.out.println("ServletContext 上下文初始化");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("ServletContext 上下文销毁");
}
}