[Spring]web.xml中配置ContextLoaderListener监听器的作用
来源:互联网 发布:微信小程序 php服务端 编辑:程序博客网 时间:2024/05/19 01:10
在Spring的核心配置文件中,为什么配置ContextLoaderListener监听器
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
打开ContextLoaderListener的源码,发现ContextLoaderListener实现了ServletContextListener接口
/* */ public class ContextLoaderListener extends ContextLoader/* */ implements ServletContextListener/* */ {/* */ public ContextLoaderListener()/* */ {/* */ }/* */ /* */ public ContextLoaderListener(WebApplicationContext context)/* */ {/* 98 */ super(context);/* */ }/* */ /* */ public void contextInitialized(ServletContextEvent event)/* */ {/* 106 */ initWebApplicationContext(event.getServletContext());/* */ }/* */ /* */ public void contextDestroyed(ServletContextEvent event)/* */ {/* 115 */ closeWebApplicationContext(event.getServletContext());/* 116 */ ContextCleanupListener.cleanupAttributes(event.getServletContext());/* */ }/* */ }
实现了ServletContextListener接口的作用就是当项目一经启动,就会激活实现了此接口的类方法,可以进行相关的初始化操作。
ServletContextListener接口实现了
public void contextInitialized(ServletContextEvent event)与
public void contextDestroyed(ServletContextEvent event)
两个方法,
意味着项目一经启动,会进入contextInitialized方法中,进行Spring的相关配置。并且contextInitialized方法有ServletContext参数,可以在web.xml中配置参数,用来ServletContext读取相关Spring配置文件, 一般 Dao, Service 的 Spring 配置都会在 listener 里加载。
项目退出时激活contextDestroyed方法。
结尾:
1. 如果只有 Spring mvc 的一个 Servlet,listener 可以不用。2. 但是如果用了Shiro 等,Shiro 用到的 Spring 的配置必须在 listener 里加载。3. 一般 Dao, Service 的 Spring 配置都会在 listener 里加载,因为可能会在多个 Servlet 里用到,因为父子 Context 的可见性问题,防止重复加载所以在 listener 里加载。所以,有时可用可不用,有时必用,具体看情况。
1 0
- [Spring]web.xml中配置ContextLoaderListener监听器的作用
- [Spring]web.xml中配置ContextLoaderListener监听器的作用
- spring项目中,web.xml中的 ContextLoaderListener监听器的原理
- Spring :监听器ContextLoaderListener的作用
- Spring监听器ContextLoaderListener的作用
- Spring监听器ContextLoaderListener的作用
- Spring监听器ContextLoaderListener的作用
- Spring :监听器ContextLoaderListener的作用
- JavaEE 下面使用Spring 时 web.xml 中ContextLoaderListener的作用及配置,多个配置文件时contextConfigLocation的作用
- web.xml文件中配置spring的ContextLoaderListener后报错
- web.xml配置文件中ContextLoaderListener的作用
- spring项目中监听器作用-ContextLoaderListener
- spring项目中监听器作用-ContextLoaderListener
- 监听器ContextLoaderListener的作用
- 监听器的作用-ContextLoaderListener
- spring中ContextLoaderListener的作用
- web.xml配置spring监听器
- web.xml中spring如何通过ContextLoaderListener初始化的
- 由Monkey测试引发的跨多个进程的Android系统死锁问题分析
- android程序改为用系统签名
- android中wifi原理及流程分析
- IM推送Android客户端之重连策略
- python 在不同层级目录import 模块的方法
- [Spring]web.xml中配置ContextLoaderListener监听器的作用
- 三范式
- HTML第四章 上机5
- 进程空间-徐明伟
- Sublime Text 格式化代码
- Android DIY之路 (一) 指定区域多图片合成 放大 缩小 镜像 旋转 等
- 初学python的小知识
- 04上机练习4
- Java NIO和IO的主要区别