(3) Create a ApplicationContext object declaratively

Source: Internet
Author: User

Time is the most precious wealth of all wealth. --De Aufrasdor


5.14.4 convenient in the %spring%/docs/spring-framework-reference/htmlsingle/index.html file ApplicationContext instantiation for Web applications.


If you want to create an instance of ApplicationContext , there are two ways to do this: one is to use code , and the other is to use a declarative approach.


Creating a ApplicationContext instance using code

ApplicationContext ac = new Classpathxmlapplicationcontext ("Com/rk/spring/applicationcontext.xml");


In Web. XML, create an ApplicationContext instance using the Contextloader declaration method

<!--spring Configuration-<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 can also create ApplicationContext instances programmatically by using one of the ApplicationContext Impleme Ntations.



In the Web. xml file, there are two ways to instantiate the ApplicationContext using the Contextloader declaration method:Contextloaderlistener and Contextloaderservlet. Highlighting Contextloaderlistener, we can see that it implements the Servletcontextlistener interface.

public class Contextloaderlistener extends Contextloader implements Servletcontextlistener {//}


The Servletcontextlistener interface is defined as follows:

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 There are some differences between servlet 2.3 and servlet 2.4. for a servlet context for Web application, once the servlet context is created, The object that implements the Servletcontextlistener interface (here refers to Contextloaderlistener) executes the contextinitialized (Servletcontextevent sce) method immediately, And once the servlet context is closed, the object that implements the Servletcontextlistener interface (here refers to Contextloaderlistener) executes the contextdestroyed immediately ( Servletcontextevent sce) method .


For spring ApplicationContext, ApplicationContext instantiation in an object in the Servletcontextlistener interface is a ideal place. Contextloaderlistener and Contextloaderservlet both achieve the same functionality, you should be inclined to use Contextloaderlistener.


The contextloader mechanism comes in the flavors:the Contextloaderlistener and the Contextloaderservlet. They has the same functionality but differ in and the listener version is not reliable in Servlet 2.3 containers. In the Servlets 2.4 specification, servlet context listeners must execute immediately after the servlet context for the web Application is created and was available to service the first request (and also when the Servlet context was about to be sh UT 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, there is a look in the Javadoc for the Contextloaderservlet.




You can use Contextloaderlistener to register ApplicationContext instances:

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



The Contextloaderlistener will detect the Contextconfiglocation parameter. If the contextconfiglocation parameter does not exist,/web-inf/applicationcontext.xml is used as the default value. If the contextconfiglocation parameter does exist and there are multiple values, the middle can be delimited using predefined delimiters (comma, semicolon, and whitespace). In the contextconfiglocation parameter, Ant-style path patterns is supported, for example,/web-inf/* Context.xml represents all files ending with context.xml in the Web-inf directory.


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 whit Espace) and uses the values as locations where application contexts would be searched. Ant-style path patterns is 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 the files in any subdirectory of "Web-inf".



We can also use Contextloaderservlet, which also uses the Contextconfiglocation parameter.

You can use Contextloaderservlet instead of Contextloaderlistener. The Servlet uses the contextconfiglocation parameter just as the listener does.


Ant Path Matching principle

Path matching principles (paths Matching) in Spring mvc are much more flexible than standard Web. Xml.

The default policy implements the Org.springframework.util.AntPathMatcher, as the name suggests, the path pattern is the Apache ant style path, Apache There are three wildcard matching methods for the path of the ant style. These can be combined with a variety of flexible path patterns

? Match any single character * Match 0 or any number of characters * * Match 0 or more directories


Example Ant-style Path Patterns

/app/*.x Match (Matches) all the. x files in the app path/app/p?ttern match (Matches)/app/pattern and/app/pxttern, but does not include/app/pttern/**/example Match (Matches)/app/example,/app/foo/example, and/example/app/**/dir/file. Match (Matches)/app/dir/file.jsp,/app/foo/dir/file.html,/app/foo/bar/dir/file.pdf, and/app/dir/file.java/**/*.jsp match ( Matches) any. jsp file










(3) Create a ApplicationContext object declaratively

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.