spring项目中监听器作用.docx

上传人:b****5 文档编号:8340163 上传时间:2023-01-30 格式:DOCX 页数:10 大小:16.10KB
下载 相关 举报
spring项目中监听器作用.docx_第1页
第1页 / 共10页
spring项目中监听器作用.docx_第2页
第2页 / 共10页
spring项目中监听器作用.docx_第3页
第3页 / 共10页
spring项目中监听器作用.docx_第4页
第4页 / 共10页
spring项目中监听器作用.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

spring项目中监听器作用.docx

《spring项目中监听器作用.docx》由会员分享,可在线阅读,更多相关《spring项目中监听器作用.docx(10页珍藏版)》请在冰豆网上搜索。

spring项目中监听器作用.docx

spring项目中监听器作用

spring项目中监听器作用

1spring框架的启动入口ContextLoaderListener

 

2

作用:

在启动Web

容器时,自动装配Spring

applicationContext.xml

的配置信息。

因为它实现了ServletContextListener

这个接口,在web.xml

配置这个监听器,启动容器时,就会默认执行它实现的方法。

在ContextLoaderListener

中关联了ContextLoader

这个类,所以整个加载配置过程由ContextLoader

来完成

pring

web

下的入口在配置文件

web.xml

的监听器中

<

listener

>

<

listener-class

>

org.springframework.web.context.ContextLoaderListener

</

listener-class

>

</

listener

>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:

conf/spring/applicationContext.xml</param-value>

</context-param>

上述是在web.xml

中的配置信息。

//

实现了接口

ServletContextListener,

也就是说他必须实现

contextDestroyed,contextInitialized

这两个方法

public

classContextLoaderListenerimplementsServletContextListener

{

 

private

ContextLoadercontextLoader;

/**

*

Initializetherootwebapplicationcontext.

*/

//Spring

框架由此启动

contextInitialized

也就是监听器类的

main

入口函数

public

voidcontextInitialized

(ServletContextEventevent){

this.contextLoader=createContextLoader();

this.contextLoader.initWebApplicationContext(event.getServletContext());

}

/**

*Create

theContextLoadertouse.Canbeoverriddeninsubclasses.

*@return

thenewContextLoader

*/

 

protected

ContextLoadercreateContextLoader(){

returnnewContextLoader();

}

/**

*Return

theContextLoaderusedbythislistener.

*@return

thecurrentContextLoader

*/

public

ContextLoadergetContextLoader(){

returnthis.contextLoader;

}

/**

*Close

therootwebapplicationcontext.

*/

public

voidcontextDestroyed

(ServletContextEventevent){

if(this.contextLoader!

=null){

this.contextLoader.closeWebApplicationContext(event.getServletContext());

}

}

}

总的来说这个入口非常简单

所有实现都隐藏在

ContextLoader

类里

我们在下一篇的内容中讨论

ContextLoader,

如果你不知道为什么这里是程序的入口

那么复习一下

ServletContextListener

接口和监听器的相关知识吧

ServletContext

Servlet

程序用来与Web

容器通信。

例如写日志,转发请求。

每一个Web

应用程序含有一个Context

,被Web

应用内的各个程序共享。

因为Context

可以用来保存资源并且共享,所以我所知道的ServletContext

的最大应用是Web

缓存----

把不经常更改的内容读入内存,所以服务器响应请求的时候就不需要进行慢速的磁盘I/O

了。

ServletContextListener

ServletContext

的监听者,如果ServletContext

发生变化,如服务器启动时ServletContext

被创建,服务器关闭时ServletContext

将要被销毁。

在JSP

文件中,application

是ServletContext

的实例,由JSP

容器默认创建。

Servlet

中调用getServletContext()

方法得到ServletContext

的实例。

我们使用缓存的思路大概是:

1.

服务器启动时,ServletContextListener

contextInitialized()

方法被调用,所以在里面创建好缓存。

可以从文件中或者从数据库中读取取缓存内容生成类,用ervletContext.setAttribute()

方法将缓存类保存在

ServletContext

的实例中。

2.

程序使用ServletContext.getAttribute()

读取缓存。

如果是JSP

,使用application.getAttribute()

如果是Servlet

,使用getServletContext().getAttribute()

如果缓存发生变化(

如访问计数)

,你可以同时更改缓存和文件/

数据库。

或者你等变化积累到一定程序再保存,也可以在下一步保存。

3.

服务器将要关闭时,ServletContextListener

的contextDestroyed()

方法被调用,所以在里面保存缓存的更改。

将更改后的缓存保存回文件或者数据库,更新原来的内容。

Java

代码importUser;//myown

 

classimportDatabaseManager;//myownclass

 

importjavax.servlet.ServletContext;

 

importjavax.servlet.ServletContextListener;

 

publicclassMyContextListenerimplementsServletContextListener{

 

privateServletContextcontext=null;

 

publicvoidcontextInitialized(ServletContextEventevent){

 

context=event.getServletContext();

 

Useruser=DatabaseManager.getUserById

(1);

 

context.setAttribute("user1",user);

 

}

 

publicvoidcontextDestroyed(ServletContextEventevent){

 

Useruser=(User)context.getAttribute("user1");

 

DatabaseManager.updateUserData(user);

 

this.context=null;

 

}

 

}

 

importUser;//myown

classimportDatabaseManager;//

myownclass

import

javax.servlet.ServletContext;

import

javax.servlet.ServletContextListener;

publicclass

MyContextListener

implements

ServletContextListener{

privateServletContextcontext=null;

publicvoid

contextInitialized(ServletContextEventevent){

context=

event.getServletContext();

Useruser=

DatabaseManager.getUserById

(1);

context.setAttribute("user1",user);

}

publicvoid

contextDestroyed(ServletContextEventevent){

Useruser=

(User)context.getAttribute("user1");

DatabaseManager.updateUserData(user);

this.context=null;

}

}

布署ServletContextListener

 

你实现(implements)

了ServletContextListener

编译后,把它放在正确的WEB-INF/classes

目录下,更改WEB-INF

目录下的web.xml

文件,在web-app

节点里添加

<listener>

<listener-class>MyServletContextListener</listener-class>

</listener>

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1