`

Spring 启动 ContextLoaderListener

 
阅读更多

1, ContextLoaderListener

 

ContextLoaderListener implements ServletContextListener


private ContextLoader contextLoader;


// 这个方法在Web应用服务做好接受请求的时候被调用
public void contextInitialized(ServletContextEvent event) {
		this.contextLoader = createContextLoader();
		this.contextLoader.initWebApplicationContext(event.getServletContext());
	}


public void contextDestroyed(ServletContextEvent event) {
		if (this.contextLoader != null) {
			this.contextLoader.closeWebApplicationContext(event.getServletContext());
		}
	}

 ServletContextListener的用法。这个事件类作为Web应用服务的一部分,处理Web应用的 servlet上下文(context)的变化的通知。这可以解释为,好像有个人在服务器旁不断地通知我们服务器在发生什么事件。那当然需要监听者了。因此,在通知上下文(context)初始化和销毁的时候,ServletContextListner非常有用

ServletContextListener接口有两方需要实现的方法:
contextInitialized()contextDestroyed();

我们可以利用这个特性初始化一些信息,当然我们也可以利用Servlet类init()方法,并在配置文件中让它启动应用的时候
就执行,并且在关闭的时候执行destroy()方法.但是继承此接口应该更符合容器的应用.

 

 

 

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>


//对于实现了Servlet 2.2规范的Web容器而言,只能够使用ContextLoaderServlet。对于一些实现了Servlet 2.3规//范的Web容器而言,也可以使用ContextLoaderListener。

<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>


 

 

分享到:
评论

相关推荐

    Spring的监听器ContextLoaderListener的作用

    ContextLoaderListener 是 Spring 框架中的一种监听器,它的主要作用是启动 Web 容器时,自动装配 ApplicationContext 的配置信息。它实现了 ServletContextListener 接口,在 web.xml 文件中配置这个监听器,启动...

    springweb3.0MVC注解(附实例)

    org.springframework.web.context.ContextLoaderListener </listener> <!-- Spring MVC 的Servlet,它将加载WEB-INF/annomvc-servlet.xml 的 配置文件, 以启动Spring MVC模块--> <servlet-name>annomvc ...

    spring源代码解析

    Spring的ContextLoader是提供这样性能的类,我们可以使用 ContextLoaderServlet或者ContextLoaderListener的启动时载入的Servlet来实例化Spring IOC容器 – 为什么会有两个不同的类来装载它呢,这是因为它们的使用...

    Spring与Web环境集成.pdf

    为了解决这些弊端,我们可以使用ServletContextListener监听Web应用的启动,在Web应用启动时加载Spring的配置文件,创建应用上下文对象ApplicationContext,并将其存储到ServletContext域中。这样,我们就可以在任意...

    spring_MVC源码

    09. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 10. </listener> 11. 12. <servlet> 13. <servlet-name>spring</servlet-name> 14. <servlet-class>org.spring...

    Spring源码学习七:web应用自动装配Spring配置文件1

    在web应用启动读取web.xml时,发现配置了ContextLoaderListener,而ContextLoaderListener实现了ServletCo

    ssh(structs,spring,hibernate)框架中的上传下载

    Struts+Spring+Hibernate实现上传下载    本文将围绕SSH文件上传下载的主题,向您详细讲述如何开发基于SSH的Web程序。SSH各框架的均为当前最新版本:  •Struts 1.2  •Spring 1.2.5  •Hibernate 3.0  本文...

    SSH第7章上机.zip ACCP8.0

    下面是整合的的步骤 ...<listener-class>org.springframework.web.context.ContextLoaderListener 在struts.xml中配置对象创建工具为spring <constant name="struts.objectFactory" value="spring" />

    Proxool-0.9.1

    proxool.0.9.1基础上做了修改。 改jar名称为:proxool-0.9.1.1,主要修改为以下3点: ... <listener-class>org.springframework.web.context.ContextLoaderListener 4.修正了proxool在多线程环境下的的错误。

    MAVEN构建的工程无法发布lib到tomcat的解决方

    可能会遇到发布到Tomcat服务器的困难,Error configuring application listener of class org.springframework.web.context.ContextLoaderListener错误,无法加载相应的 jar 文件,导致项目无法正常启动。...

    java异常解决方案.docx

    * java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener + 解决方案:检查 Spring 的依赖关系,是否存在类加载的问题,尝试使用 Dependency Inject 来解决问题。 * Exception...

    java微信公众号MVC开发框架

    <listener-class>org.springframework.web.context.ContextLoaderListener <servlet-name>weixin <servlet-class>com.github.jweixin.jwx.servlet.WeixinDispatcherServlet</servlet-class> ...

    基于EXT SSI的简单树实现

    <listener-class>org.springframework.web.context.ContextLoaderListener <description>spring初始配置 <param-name>contextConfigLocation <param-value>classpath:conf/spring/application.xml ...

Global site tag (gtag.js) - Google Analytics