標籤: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進行執行個體化,有兩種方式:ContextLoaderListener和ContextLoaderServlet。重點介紹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 * @sincev 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對象