Spring在Web应用中的事件与使用
来源:互联网 发布:nginx 表达式 编辑:程序博客网 时间:2024/06/06 18:36
ApplicationContext在WEB应用中的实例化
ApplicationContext能以声明的方式创建,如使用ContextLoader。当然你也可以使用以编程的方式创建ApplicationContext实例。首先,让我们先分析ContextLoader接口及其实现。ContextLoader机制有两种方式,ContextLoaderListener 和ContextLoaderServlet,他们功能相同但是listener不能在Servlet2.3容器下使用。Servlet2.4规范中servlet context listeners需要在web应用启动并能处理初始请求时立即运行。(servlet context listener关闭的时候也是相同的)。servlet context listener是初始化Spring ApplicationContext理想的方式。
可以象下面所示例的一样使用ContextLoaderListener注册一个ApplicationContext
<context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- or use the ContextLoaderServlet instead of the above listener<servlet><servlet-name>context</servlet-name><servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class><load-on-startup>1</load-on-startup></servlet>-->
监听器首先检查contextConfigLocation参数,如果它不存在,它将使用/WEB-INF/applicationContext.xml作为默认值。如果已存在,它将使用分隔符(逗号、冒号或空格)将字符串分解成应用上下文件位置路径。可以支持ant-风格的路径模式,如/WEB-INF/*Context.xml(WEB-INF文件夹下所有以"Context.xml"结尾的文件)。或者/WEB-INF/**/*Context.xml(WEB-INF文件夹及子文件夹下的以"Context.xml"结尾的文件)。
ContextLoaderServlet同ContextLoaderListener一样使用'contextConfigLocation'参数。
ApplicationContext在Web应用中的获得
import org.springframework.web.context.support.WebApplicationContextUtils;ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(context);ApplicationContext事件
事件的处理通过ApplicationEvent类和ApplicationListener接口来提供的。如果在上下文中部署一个实现了ApplicationListener接口的bean,那么每当一个ApplicationEvent发布到ApplicationContext时,这个bean就得到通知。实质上,这是标准的Observer设计模式。
事件类型如下
使用举例:
//applicationContext.xml中部署listener bean<bean id="workloadListener" class="WorkloadListener"></bean>
public class WorkloadListener implements ApplicationListener{public void onApplicationEvent(ApplicationEvent event) { if(event instanceof ContextRefreshedEvent) ...; if(event instanceof ContextClosedEvent) ...;}}
- Spring在Web应用中的事件与使用
- Spring在Web中的应用
- quartz在web项目中的应用(不使用spring)
- 【Spring】在WEB应用中使用Spring
- Spring在Web应用中的初始化
- spring在web应用中的配置
- Spring在Web应用中的配置
- Spring 在web应用中的定时器
- Spring在web开发中的应用
- Spring在web开发中的应用
- Spring在web开发中的应用与Spring整合junit4测试
- spring事件驱动模型--观察者模式在spring中的应用
- Spring 在 WEB 应用中使用
- 在web应用中使用Spring
- Spring在web应用中使用?
- Spring_35_在 WEB 应用中使用 Spring
- 在WEB应用中使用spring
- Spring中的事件监听机制在项目中的应用
- 数据库理论
- 希尔排序
- JAVA中断言的使用
- ASP.NET C# 日期 时间 年 月 日 时 分 秒 格式及转换
- Remove Security Shield Virus
- Spring在Web应用中的事件与使用
- 排序比较函数
- Android SQLite
- Android Stagefright MPEG4Extractor分析
- Web开发人员的6种赚钱之道
- 计算机字、字节、字长之某腾讯面试题
- 四种负载均衡技术大比拼
- 启动tomcat后Myeclipse调试Class<T>.getDeclaredConstructors0 的原因
- x264.exe下载