Spring WebApplicationContext初始化

如果要在我们的javaweb项目中引入Spring框架,首先我们需要将我们需要引入的Spring对应版本的jar包引入到我们的项目工程中,我这里是使用maven来管理jar包的,所以需要在pom.xml文件中添加如下依赖配置:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-webmvc</artifactId>

     <version>4.3.0.RELEASE</version>

</dependency>

注:我这里使用的Spring版本是4.3.0

添加完成之后我们的Maven Dependencies目录是这样的:

 

注:除了第一个jar包,这些是javaweb工程中引入Spring需要的最少的jar包。

完了之后,想要在我们的项目中实际运用到Spring,我们还需要在web.xml文件中对引入spring做以下相关配置:

<!--配置spring容器与本地数据库文件-->

<context-param>

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

<param-value>/WEB-INF/bean.xml</param-value>

</context-param>

<!--配置spring容器监听-->

<listener>

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

</listener>

仅仅只是以上的配置信息,还不能是我们的项目使用完整的SpringMVC框架,所以我们还要配置以下信息:

<!-- 配置springMVC拦截器  -->

<servlet>

<servlet-name>SpringServlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>SpringServlet</param-name>

<param-value>/WEB-INF/SpringServlet-servlet.xml</param-value>

</init-param>

</servlet>

以下是为何要配置这些信息的解释,以及Web容器加载并初始化spring的步骤:

一、org.springframework.web.context.ContextLoaderListener详解

官方API解释:

public class ContextLoaderListener

extends ContextLoader

implements ServletContextListener

用来引导启动和关闭Spring的根WebApplicationContext的监听器。简单的委托给ContextLoader以及ContextCleanupListener。(言外之意就是,启动和关闭的动作实际上并不是由ContextLoaderListener来实现的,而是将其委托给了其他组件)

web.xml文件中,ServletContextListener应该注册在Log4jConfigListener(如果要使用Log4jConfigListener的话)。

(注:3.0以后就不再使用Log4jConfigListenerContextLoaderServlet。)

Spring3.1中,ContextLoaderListener支持通过构造方法ContextLoaderListener(WebApplicationContext context)来注入根web应用上下文,允许编程配置Servlet 3.0 +环境中。

 

ContextLoaderListener中有一个方法:contextInitialized(ServletContextEvent event),源码如下:

/**

 * Initialize the root web application context.

 */

@Override

public void contextInitialized(ServletContextEvent event) {

initWebApplicationContext(event.getServletContext());

}

由此可见,初始化WebAppliactionContext的工作是在initWebApplicationContext方法中完成的。

二、org.springframework.web.context.ContextLoader详解

ContextLoader这个类用于初始化WebApplicationContext,并且是在ContextLoaderListener中调用初始化。

ContextLoader官方API说明如下:

通过被ContextLoaderListener调用来为根应用程序上下文执行实际的初始化工作。查找在web.xml文件中context-param节点的“contextClass”参数来指定上下文类类型(context class type),如果没有该参数则使用默认的XmlWebApplicationContext。作为默认的ContextLoader实现,任何被指定的context class都必须实现ConfigurableWebApplicationContext接口。

处理<context-param>标签中的参数"contextConfigLocation"并将它的值传递到 context instance,而这个参数的值可能是有逗号或空格分隔的多个文件路径,例如:"WEB-INF/applicationContext1.xml, WEB-INF/applicationContext2.xml"Ant-style路径模式也是支持的,例如:"WEB-INF/*Context.xml,WEB-INF/spring*.xml"或者"WEB-INF/**/*Context.xml"。如果没有显示的指定该参数,则会使用XMLWebApplicationContext类中配置的默认的配置文件路径:"/WEB-INF/applicationContext.xml"。源码如下:

/** Default config location for the root context */

public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";

 

 

ContextLoader的类使用到了下面的context-param配置信息:

1、contextClass

初始化时,会在web.xml文件中的<context-param>标签中查找是否有一个叫做contextClass的配置。这个配置是用于自定义WebApplicationContext的实现类(自定义的WebApplicationContext实现类必须实现ConfigurableWebApplicationContext接口)。如果没有指定这个上下文参数,就默认使用XmlWebApplicationContext这个类。

2、contextConfigLocation

这个参数用于指定applicationContext*.xml文件的位置。在这个参数中可以设置多个文件的路径,互相用逗号或空格来分隔;也可以用通配符的方式设置多个文件的路径。如果不在context-param标签中设置contextConfigLocation参数,则使用默认值:/WEB-INF/applicationContext.xml。(注:在实际的项目中,如果指定了多个applicationContext.xml文件,在多个文件中如有重名的<bean>,就会自动让后面的覆盖前面的<bean>)。

另外,这两个参数的名字都是固定不变的:原因在ContextLoader类中,源码如下:

/**

 * Name of servlet context parameter (i.e., {@value}) that can specify the

 * config location for the root context, falling back to the implementation's

 * default otherwise.

 * @see org.springframework.web.context.support.XmlWebApplicationContext#DEFAULT_CONFIG_LOCATION

 */

public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";

 

/**

 * Config param for the root WebApplicationContext implementation class to use: {@value}

 * @see #determineContextClass(ServletContext)

 */

public static final String CONTEXT_CLASS_PARAM = "contextClass";


本文只是对web容器加载spring做了初步且浅显的探索,spring的博大精深不是一朝一夕就能参透的,所以深层的原理还需要时间去摸索。文中可能存在遗漏或偏差,还请各位不吝赐教。另外,本文参考了一些相关文章的内容,所以首先要感谢这些博主的文章。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值