SpringBoot中的ServletContextListener接口实现类

 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缓存初始化

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞翔的咩咩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值