Spring中ContextLoaderListener类实现了ServletContextListener接口
可以看到有
在SpringBoot中用来初始化WebApplicationContext。
/** * Initialize the root web application context. */ @Override public void contextInitialized(ServletContextEvent event) { initWebApplicationContext(event.getServletContext()); }
实现这个接口还能处理一些启动前和启动后,或者销毁后的一些事情。例如
用来将数据库中数据缓存到redis中
import com.rongsoft.eurekacenter.eurekacenter.config.Person;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.loadbalancer.core.LoadBalancerServiceInstanceCookieTransformer;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.Enumeration;
/**
* 可以实现ContextLoaderListener 也可以实现ServletContextListener,结果一致
* 需要添加@ServletComponentScan 注解
*/
@Slf4j
@Component
@WebListener
public class SpringServletContextListener implements ServletContextListener {
// 这个autowired无效,不知道为何
@Autowired
@Qualifier("stringRedisTemplate")
private StringRedisTemplate stringRedisTemplate;
@Override
public void contextInitialized(ServletContextEvent event) {
ServletContext servletContext = event.getServletContext();
Enumeration<String> enumeration = servletContext.getAttributeNames();
while (enumeration.hasMoreElements()){
String element = enumeration.nextElement();
log.info("-----ContextLoaderListener-----"+element);
if (WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE.equals(element)){
// 这个就是ApplicationContext
ApplicationContext context = (ApplicationContext) servletContext.getAttribute(element);
stringRedisTemplate = context.getBean(StringRedisTemplate.class);
log.info("从ServletContext获取ApplicationContext:"+context.getBean(Person.class));
log.info("从ServletContext获取ApplicationContext:"+stringRedisTemplate);
String name = (String) stringRedisTemplate.opsForValue().get("name");
log.info("从redis获取数据:RedisTemplate:"+name);
}
}
// 通过servlet获取ApplicationContext
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
log.info("-----ContextLoaderListener---"+applicationContext.getBean(Person.class));
log.info("-----ContextLoaderListener---"+applicationContext.getBean(LoadBalancerServiceInstanceCookieTransformer.class));
log.info("-----ContextLoaderListener---"+applicationContext.getBean(StringRedisTemplate.class));
stringRedisTemplate = applicationContext.getBean(StringRedisTemplate.class);
Enumeration<String> enumeration1 = servletContext.getAttributeNames();
while (enumeration1.hasMoreElements()){
log.info("-----ContextLoaderListener-----"+enumeration1.nextElement());
}
String name = (String) stringRedisTemplate.opsForValue().get("name");
log.info("从redis获取数据:RedisTemplate:"+name);
}
@Override
public void contextDestroyed(ServletContextEvent event) {
log.info("-----销毁---");
}
}
参数如下
spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.lettuce.pool.max-idle=8 spring.redis.lettuce.pool.max-active=8 spring.redis.lettuce.pool.max-wait=-1ms spring.redis.lettuce.pool.min-idle=0 spring.redis.lettuce.shutdown-timeout=100ms
具体使用可以参照:
ServletContextListener的作用 - 星朝 - 博客园
SpringBoot集成Redis缓存并实现初始化用户数据到Redis缓存_码出精彩-CSDN博客_redis缓存初始化