码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-07-21 22:11:57      阅读:918      评论:0      收藏:0      [点我收藏+]

标签:spring

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


%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 文件










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

标签:spring

原文地址:http://lsieun.blog.51cto.com/9210464/1828581

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!