热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SpringBoot中的ServletContextListener接口实现类

Spring中ContextLoaderListener类实现了ServletContextListener接口可以看到有在SpringBoot中用来初始化WebApplicat

 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;@Overridepublic void contextInitialized(ServletContextEvent event) {ServletContext servletContext = event.getServletContext();Enumeration enumeration = servletContext.getAttributeNames();while (enumeration.hasMoreElements()){String element = enumeration.nextElement();log.info("-----ContextLoaderListener-----"+element);if (WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE.equals(element)){// 这个就是ApplicationContextApplicationContext 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获取ApplicationContextApplicationContext 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 enumeration1 = servletContext.getAttributeNames();while (enumeration1.hasMoreElements()){log.info("-----ContextLoaderListener-----"+enumeration1.nextElement());}String name = (String) stringRedisTemplate.opsForValue().get("name");log.info("从redis获取数据:RedisTemplate:"+name);}@Overridepublic 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缓存初始化


推荐阅读
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 标题: ... [详细]
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
  • 本文介绍了将mysql从5.6.15升级到5.7.15的详细步骤,包括关闭访问、备份旧库、备份权限、配置文件备份、关闭旧数据库、安装二进制、替换配置文件以及启动新数据库等操作。 ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
  • SpringBoot整合SpringSecurity+JWT实现单点登录
    SpringBoot整合SpringSecurity+JWT实现单点登录,Go语言社区,Golang程序员人脉社 ... [详细]
  • 开发笔记:spring boot项目打成war包部署到服务器的步骤与注意事项
    本文介绍了将spring boot项目打成war包并部署到服务器的步骤与注意事项。通过本文的学习,读者可以了解到如何将spring boot项目打包成war包,并成功地部署到服务器上。 ... [详细]
  • .NetCoreWebApi生成Swagger接口文档的使用方法
    本文介绍了使用.NetCoreWebApi生成Swagger接口文档的方法,并详细说明了Swagger的定义和功能。通过使用Swagger,可以实现接口和服务的可视化,方便测试人员进行接口测试。同时,还提供了Github链接和具体的步骤,包括创建WebApi工程、引入swagger的包、配置XML文档文件和跨域处理。通过本文,读者可以了解到如何使用Swagger生成接口文档,并加深对Swagger的理解。 ... [详细]
  • 本文介绍了PhysioNet网站提供的生理信号处理工具箱WFDB Toolbox for Matlab的安装和使用方法。通过下载并添加到Matlab路径中或直接在Matlab中输入相关内容,即可完成安装。该工具箱提供了一系列函数,可以方便地处理生理信号数据。详细的安装和使用方法可以参考本文内容。 ... [详细]
  • 单点登录原理及实现方案详解
    本文详细介绍了单点登录的原理及实现方案,其中包括共享Session的方式,以及基于Redis的Session共享方案。同时,还分享了作者在应用环境中所遇到的问题和经验,希望对读者有所帮助。 ... [详细]
author-avatar
许先生不会再想过去的事观_307
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有