(3)通过声明的方式创建ApplicationContext对象

时间是一切财富中最宝贵的财富。 —— 德奥弗拉斯多

%spring%/docs/spring-framework-reference/htmlsingle/index.html文件的 5.14.4 Convenient ApplicationContext instantiation for web applications

如果要创建ApplicationContext的实例,有两种方法:一种是使用代码的方式,另一种是使用声明的方式

使用代码的方式创建ApplicationContext实例

ApplicationContext ac = new ClassPathXmlApplicationContext("com/rk/spring/applicationContext.xml");

在web.xml中,使用ContextLoader的声明方式创建ApplicationContext实例

 	<!-- spring 配置 -->
 	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/classes/beans-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
You can create ApplicationContext instances declaratively by using, for example, a ContextLoader. Of course you can also create ApplicationContext instances programmatically by using one of the ApplicationContext implementations.

在web.xml文件中,使用ContextLoader的声明方式来对ApplicationContext进行实例化,有两种方式:ContextLoaderListenerContextLoaderServlet。重点介绍ContextLoaderListener,我们可以看到,它实现了ServletContextListener接口。

public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
	//
}

ServletContextListener接口的定义如下:

package javax.servlet;

import java.util.EventListener;

	/** 
	 * Implementations of this interface receive notifications about
	 * changes to the servlet context of the web application they are
	 * part of.
	 * To receive notification events, the implementation class
	 * must be configured in the deployment descriptor for the web
	 * application.
	 * @see ServletContextEvent
	 * @since	v 2.3
	 */

public interface ServletContextListener extends EventListener {
	/**
	 ** Notification that the web application initialization
	 ** process is starting.
	 ** All ServletContextListeners are notified of context
	 ** initialization before any filter or servlet in the web
	 ** application is initialized.
	 */

    public void contextInitialized ( ServletContextEvent sce );

	/**
	 ** Notification that the servlet context is about to be shut down.
	 ** All servlets and filters have been destroy()ed before any
	 ** ServletContextListeners are notified of context
	 ** destruction.
	 */
    public void contextDestroyed ( ServletContextEvent sce );
}

ContextLoaderListener在Servlet 2.3和Servlet 2.4之间存在一些差别。对于web application的Servlet context来说,一旦Servlet Context被创建,实现了ServletContextListener接口的对象(这里是指ContextLoaderListener)会马上执行contextInitialized ( ServletContextEvent sce )方法,而且一旦Servlet Context被关闭,实现了ServletContextListener接口的对象(这里是指ContextLoaderListener)会马上执行contextDestroyed ( ServletContextEvent sce )方法

对于Spring ApplicationContext来说,在ServletContextListener接口的对象中进行ApplicationContext实例化是一个ideal place。ContextLoaderListener和ContextLoaderServlet两者实现一样的功能,你更应该倾向于使用ContextLoaderListener

The ContextLoader mechanism comes in two flavors: the ContextLoaderListener and the ContextLoaderServlet. They have the same functionality but differ in that the listener version is not reliable in Servlet 2.3 containers. In the Servlet 2.4 specification, Servlet context listeners must execute immediately after the Servlet context for the web application is created and is available to service the first request (and also when the Servlet context is about to be shut down). As such a Servlet context listener is an ideal place to initialize the Spring ApplicationContext. All things being equal, you should probably prefer ContextLoaderListener; for more information on compatibility, have a look at the Javadoc for the ContextLoaderServlet.

可以使用ContextLoaderListener注册ApplicationContext实例:

You can register an ApplicationContext using the ContextLoaderListener as follows:

<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>
-->

ContextLoaderListener会检测contextConfigLocation参数。如果contextConfigLocation参数不存在,就会使用/WEB-INF/applicationContext.xml作为默认值。如果contextConfigLocation参数确实存在,而且存在多个值,中间可以使用predefined delimiters (comma, semicolon and whitespace)进行分隔。在contextConfigLocation参数中,支持Ant-style path patterns,例如,/WEB-INF/*Context.xml表示在WEB-INF目录下所有以Context.xml结尾的文件。

The listener inspects the contextConfigLocation parameter. If the parameter does not exist, the listener uses /WEB-INF/applicationContext.xml as a default. When the parameter does exist, the listener separates the String by using predefined delimiters (comma, semicolon and whitespace) and uses the values as locations where application contexts will be searched. Ant-style path patterns are supported as well. Examples are /WEB-INF/*Context.xml for all files with names ending with "Context.xml", residing in the "WEB-INF" directory, and /WEB-INF/**/*Context.xml, for all such files in any subdirectory of "WEB-INF".

我们也可以使用ContextLoaderServlet,它同样也是使用contextConfigLocation参数。

You can use ContextLoaderServlet instead of ContextLoaderListener. The Servlet uses the contextConfigLocation parameter just as the listener does.

Ant path 匹配原则

路径匹配原则(Path Matching) Spring MVC中的路径匹配要比标准的web.xml要灵活的多。

默认的策略实现了 org.springframework.util.AntPathMatcher,就像名字提示的那样,路径模式是使用了Apache Ant的样式路径,Apache Ant样式的路径有三种通配符匹配方法.这些可以组合出很多种灵活的路径模式

? 匹配任何单字符  
* 匹配0或者任意数量的字符  
** 匹配0或者更多的目录

Example Ant-Style Path Patterns

/app/*.x 匹配(Matches)所有在app路径下的.x文件  
/app/p?ttern 匹配(Matches) /app/pattern 和 /app/pXttern,但是不包括/app/pttern  
/**/example 匹配(Matches) /app/example, /app/foo/example, 和 /example  
/app/**/dir/file. 匹配(Matches) /app/dir/file.jsp, /app/foo/dir/file.html,/app/foo/bar/dir/file.pdf, 和 /app/dir/file.java  
/**/*.jsp 匹配(Matches)任何的.jsp 文件
时间: 2024-08-11 19:12:17

(3)通过声明的方式创建ApplicationContext对象的相关文章

5 -- Hibernate的基本用法 --4 1 创建Configuration对象

org.hibernate.cfg.Configuration实例代表了应用程序到SQL数据库的配置信息,Configuration对象提供了一个buildSessionFactory()方法,该方法可以产生一个不可变的SessionFactory对象. 另外,先实例化Configuration实例,然后在添加Hiberante持久化类.Configuration对象可调用addAnnotatedClass()方法逐个地添加持久化类,也可调用addPackage()方法添加指定包下的所有持久化类

于Unity3D动态创建对象和创建Prefab三种方式的原型对象

u3d在动态创建的对象,需要使用prefab 和创建时 MonoBehaviour.Instantiate( GameObject orignal) 需要的对象为原型. 文提供三种方式获得prefab对象. 方式一:使用脚本的public字段 直接在Project视图里找到做好的prefab,将其拖拽到指定脚本的指定public GameObject 字段. 方式二:Resource类 1.在Assets目录下的任何位置创建一个名为resources的目录.将做好的prefab放到这个目录下,p

spring中创建bean对象的三种方式以及作用范围

时间:2020/02/02 一.在spring的xml配置文件中创建bean对象的三种方式: 1.使用默认构造函数创建.在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数函数,则对象无法创建. <bean id="one" class="sdnu.machi.one"></bean> 如果one.class中没有默认构造函数则会报

JDBC 创建连接对象的三种方式

创建连接对象的三种方式 //第一种方式 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?user=root&password=root") ; //第二种方式 //读取properties文件 Properties pro = new Properties() ; InputStream in = JdbcDemo3.class.getClassLoader().ge

ApplicationContext对象的获取方式

本来这是一个比较简单的问题. 然而在前一段事件项目中出现了一个问题. 问题描述:数据库连接数只增不减,直到数据库链接数爆满,报了一个 too many connection异常.整个服务就挂了,需要重写启动一次才能使用. 一开始的时候,一直以为是dao层那个链接没有释放.把dao层的代码都查了一遍,也从网上搜索了很多关于数据库连接池的东西,但是也没有发现问题.于是就纠结了~~ 后台通过mysql的一个命令,status,发现在求职者简历编辑模块,每编辑一次简历,数据库连接数就会只增不减,增加三个

Java基础之创建实例化对象的方式

Java中创建(实例化)对象的五种方式  1.用new语句直接创建对象,这是最常见的创建对象的方法. 2.通过工厂方法返回对象,如:String str = String.valueOf(23); 3.运用反射手段,调用java.lang.Class或者java.lang.reflect.Constructor类的newInstance()实例方法.如:Object obj = Class.forName("java.lang.Object").newInstance(); 4.调用对

创建Javascript对象的途径/方式

1.通过Object对象实例化,然后在外部添加属性/方法(原始模式) var obj = new Object(); obj.v = ''; obj.func = function() { //... } 2.通过构造函数实例化,然后在外部通过prototype添加属性,或者在构造函数里面直接定义属性 (只在外部定义属性,是原型模式,只在内部定义属性,为构造函数模式,内外都定义,即混合原型/构造函数模式) //原型模式 function obj() { } obj.v = ''; obj.pro

Java反射机制(创建Class对象的三种方式)

1:SUN提供的反射机制的类: java.lang.Class<T> java.lang.reflect.Constructor<T> java.lang.reflect.Field java.lang.reflect.Method java.lang.reflect.Modifier 2:什么是反射 JAVA反射机制是在运行状态中,对于任意一个类.都能都知道这个类的所有属性和方法,对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称

SSH(Struts2+Spring+Hibernate)框架搭建流程&lt;注解的方式创建Bean&gt;

此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblogs.com/wkrbky/p/5912810.html 一.Hibernate(数据层)的搭建: 实现流程 二.Spring(注入实例)的使用: 实现流程 三.Struts2(MVC)的搭建: 实现流程 这里注意一点问题: Struts2与Hibernate在一起搭建,antlr包,有冲突.MyE