Spring的监听器ContextLoaderListener
一、作用
ContextLoaderListener监听器的作用就是启动web容器时,自动装配ApplicationContext的配置信息。它实现了ServletContextListener接口,在web.xml文件中配置这个监听器,启动容器时,就会默认执行它实现的方法。
二、ContextLoader
ContextLoaderListener关联了ContextLoader,整个加载配置过程也是由ContextLoader来完成的。
1. ContextLoader可以由ContextLoaderListener和ContextLoaderServlet生成。ContextLoaderServlet不仅继承了ContextLoader,而且也实现了HttpServlet方法。
2. ContextLoader创建了WebApplicationContext,它继承了ApplicationContext —> BeanFactory,说明Spring的所有bean也是在ContextLoader中创建的。
三、如何配置applicationContext.xml文件
1. 采用默认配置路径。将applicationContext.xml放置在WEB-INF下,不能自定义文件名。然后只需在web.xml中配置。
<!-- Spring Listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2. 指定配置文件。需要在web.xml中配置参数指定文件路径。然后仍需配置上面代码中的监听器。
<!-- 参 数:Spring配置路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
四、扩展
如果有多个xml文件,可以写在一起并用","号分隔。也可以采用通配符的形式,applicationContext-*.xml,符合条件的文件都会被一同加载。
Spring的监听器ContextLoaderListener的更多相关文章
- 配置spring的监听器 让spring随项目的启动而启动
<!-- 配置spring的监听器 让spring随项目的启动而启动 --> <listener> <listener-class>org.springframew ...
- 使用spring的监听器来完成系统超级管理员的注册
1.注入 2“util类 package com.liveyc.mgrsite.util; import org.springframework.beans.factory.annotation.Au ...
- spring boot监听器的实现
spring boot监听器的实现 如下所示: import javax.servlet.ServletContextEvent; import javax.servlet.ServletContex ...
- spring listener监听器
1.Listener的定义与作用 监听器Listener就是在application,session,request三个对象创建.销毁或者往其中添加修改删除属性时自动执行代码的功能组件. Listen ...
- 使用监听器来启动spring -------使用监听器初始化上下文参数
问题: 数据初始化监听器要注入spring容器的对象,必须先启动spring容器才能使用监听器初始化数据. 解决: 使用监听器来启动spring框架 问题:spring框架启动需要哪些参数? 1.需要 ...
- [Spring框架] Spring中的 ContextLoaderListener 实现原理.
前言: 这是关于Spring的第三篇文章, 打算后续还会写入AOP 和Spring 事务管理相关的文章, 这么好的两个周末 都在看code了, 确实是有所收获, 现在就来记录一下. 在上一篇讲解Spr ...
- Spring: DispacherServlet和ContextLoaderListener中的WebApplicationContext的关系
在Web容器(比如Tomcat)中配置Spring时,你可能已经司空见惯于web.xml文件中的以下配置代码: <context-param> <param-name>cont ...
- spring boot 监听器实例
在日常项目中订单创建成功后,会有类似各式各样的通知.有站内通知.短信通知.微信,app通知. 伪代码: 这里,只用伪代码示例.各式各样的通知 肯定不只一行代码.只是简化.如果后续还要增加各种各样的通知 ...
- Spring day03笔记
spring day02回顾 AOP :切面编程 切面:切入点 和 通知 结合 spring aop 编程 <aop:config> 方法1: <aop:pointcut expre ...
随机推荐
- Python 购物车----之用户部分
知识点: 文件读,写操作,if 判断, for 循环 salary = input("输入你的工资:") bought_list = [] product_list = {} wi ...
- SAP CRM 高效调试方法
调试,是程序开发中的基本技巧.快速定位错误消息在源代码中的位置,对发现和解决程序中的问题有着重要的意义.在SAP CRM中,错误消息通常在前台的Web Client页面中展示,应该怎样定位相关代码的位 ...
- jq 测试是否到页面最底端
$(window).scroll(function () { if ($(document).scrollTop() + $(window).height() >= $(document).he ...
- C#带参数打开网页及url获取
1.带参数打开网页 Response.Redirect("form2.aspx?id=url1&name=ok"); 其中?后面为参数. 2.获取url 命令 结果 Req ...
- Tweak 中系统方法写入文件到根目录下面失败
Tweak 中系统方法写入文件到根目录下面失败 失败原因: Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t ...
- find用法积累
查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" 查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 find .|xar ...
- Qt 解压/压缩文件
很久没有在博客园写随笔了,今天项目需要解压和压缩文件,所以去了解哈. 参考的是大神的代码:https://yq.aliyun.com/articles/24428. 使用的是 QuaZIP类. 类 说 ...
- PHP strtotime在linux服务器时间延迟8小时问题
今天客户反映有个功能投票模块第一天投了后,第二天就不能投了,理论上是第二天凌晨就可以再答题的,发现本地是正常的,linux服务器异常, 仔细查找原因发现是strtotime函数获取的值和本地获取的值不 ...
- 使用 JUnit 报错 java.lang.Exception: No runnable methods
错误详情如下: java.lang.Exception: No runnable methods at org.junit.runners.BlockJUnit4ClassRunner.validat ...
- JavaScript忍者秘籍——运行时代码求值
1. 代码求值机制 JavaScript中,有很多不同的代码求值机制. ● eval()函数 ● 函数构造器 ● 定时器 ● <script>元素 - 用eval()方法进行求值 作为定义 ...