spring在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 class ContextLoaderListener implements ServletContextListener {
private ContextLoader contextLoader;
/**
* Initialize the root web application context.
*/
//Spring框架由此启动, contextInitialized也就是监听器类的main入口函数
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
/**
* Create the ContextLoader to use. Can be overridden in subclasses.
* @return the new ContextLoader
*/
protected ContextLoader createContextLoader() {
return new ContextLoader();
}
/**
* Return the ContextLoader used by this listener.
* @return the current ContextLoader
*/
public ContextLoader getContextLoader() {
return this.contextLoader;
}
/**
* Close the root web application context.
*/
public void contextDestroyed(ServletContextEvent event) {
if (this.contextLoader != null) {
this.contextLoader.closeWebApplicationContext(event.getServletContext());
}
}
}
总的来说这个入口非常简单,所有实现都隐藏在ContextLoader类里,我们在下一篇的内容中讨论ContextLoader,如果你不知道为什么这里是程序的入口,那么复习一下ServletContextListener 接口和监听器的相关知识吧
分享到:
相关推荐
ContextLoaderListener 是 Spring 框架中的一种监听器,它的主要作用是启动 Web 容器时,自动装配 ApplicationContext 的配置信息。它实现了 ServletContextListener 接口,在 web.xml 文件中配置这个监听器,启动...
一、Web项目中使用Spring ...在 Web 项目中使用 Spring 框架,首先要解决在 web 层(这里指 Servlet)中获取到 Spring容器的问题。只要在 web 层获取到了 Spring 容器,便可从容器中获取到 Service 对象
java解决org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.ContextLoaderListener <filter-name>encodingFilter org.springframework.web.filter.CharacterEncodingFilter <param-name>encoding ...
Spring框架提供了一个监听器ContextLoaderListener,就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中。同时,Spring还提供了一个客户端工具...
<listener-class>org.springframework.web.context.ContextLoaderListener ``` * 配置context-param: ```xml <param-name>contextConfigLocation <param-value>classpath:config/applicationContext.xml ``` ...
web.xml中ContextLoaderListener的运行
Spring MVC 是一个基于 DispatcherServlet 的 MVC 框架,它是当前主流的 Web 框架之一。要想灵活运用 Spring MVC 来应对大多数的 Web 开发,就必须要掌握它的配置及原理。 一、Spring MVC 环境搭建 1. jar 包引入 ...
org.springframework.web.context.ContextLoaderListener.class org.springframework.web.context.ContextLoaderServlet.class org.springframework.web.context.ServletConfigAware.class org.springframework.web....
ApplicationContext 是 Spring 框架中的核心组件之一,负责加载和管理应用程序中的 Bean 对象。在 Web 应用程序中,ApplicationContext 的加载机制是非常重要的, Spring 提供了多种方式来加载 ApplicationContext。...
org.springframework.web.context.ContextLoaderListener 4.配置spring的配置文件(可以查看WEB-INF里面的applicationContext.xml文件) 注意:如果是默认,一定要放在WEB-INF中并文件名为...
在 Spring 框架中,获取 WebApplicationContext 和 ApplicationContext 对象是非常重要的,因为它们提供了访问 Spring 容器中的 Bean 对象的入口。下面将详细介绍五种获取 WebApplicationContext 和 ...
org.springframework.web.context.ContextLoaderListener </listener> <!-- Spring MVC 的Servlet,它将加载WEB-INF/annomvc-servlet.xml 的 配置文件, 以启动Spring MVC模块--> <servlet-name>annomvc ...
NULL 博文链接:https://angie.iteye.com/blog/2334955
这篇文章将教你快速地上手使用 Spring 框架. 如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦! 首先, 我需要在你心里建立起 Spring...
Spring framework 5.1.5源码 Main: spring-web/org.springframework.web.context.ContextLoaderListener
SSH 框架是一种常用的 Java Web 应用程序框架,它集成了 Struts、Spring 和 Hibernate 等多种技术,提供了一个强大且灵活的开发平台。下面是 SSH 框架搭建教程的知识点总结: 一、创建 WEB 工程,添加 Struts 支持 ...
SSH各框架的均为当前最新版本: •Struts 1.2 •Spring 1.2.5 •Hibernate 3.0 本文选用的数据库为Oracle 9i,当然你可以在不改动代码的情况下,通过配置文件的调整将其移植到任何具有Blob字段类型的数据库...
Spring的ContextLoader是提供这样性能的类,我们可以使用 ContextLoaderServlet或者ContextLoaderListener的启动时载入的Servlet来实例化Spring IOC容器 – 为什么会有两个不同的类来装载它呢,这是因为它们的使用...
org.springframework.web.context.ContextLoaderListener <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>