The Listener Action-contextloaderlistener (RPM) in spring project

Source: Internet
Author: User

1 Spring Frame boot entry Contextloaderlistener

2 function: Automatically assemble the spring applicationcontext.xml configuration information when the Web container is started.

Because it implements the Servletcontextlistener interface, when the Web. XML configures this listener, when the container is started, it executes the method it implements by default. Contextloader is associated with this class in Contextloaderlistener, so the entire load configuration process is done by Contextloader

Pring the portal under the Web in the Listener of config file Web. xml

< listener >

< Listener-class >

Org.springframework.web.context.ContextLoaderListener

</Listener-class >

</Listener >

<context-param>

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

<param-value>classpath:conf/spring/applicationContext.xml</param-value>

</context-param>

The above is the configuration information in Web. Xml.

Implements the interface Servletcontextlistener, that is to say he must implement Contextdestroyed, contextinitialized these two methods

public class Contextloaderlistener implements Servletcontextlistener {

Private Contextloader Contextloader;

/**

* Initialize the root Web application context.

*/

The Spring framework starts with this, and contextinitialized is the main entry function for the Listener class.

public void contextinitialized (Servletcontextevent event) {

This.contextloader = Createcontextloader ();

This.contextLoader.initWebApplicationContext (Event.getservletcontext ());

}

/**

* Create the Contextloader to use. Can is overridden in subclasses.

* @return The new Contextloader

*/

Protected Contextloader Createcontextloader () {

return new Contextloader ();

}

/**

* Return the Contextloader used by this listener.

* @return The current Contextloader

*/

Public Contextloader Getcontextloader () {

return this.contextloader;

}

/**

* Close the root Web application context.

*/

public void contextdestroyed (Servletcontextevent event) {

if (This.contextloader! = null) {

This.contextLoader.closeWebApplicationContext (Event.getservletcontext ());

}

}

}

Overall this entrance is very simple, all implementations are hidden in the Contextloader class, we discuss Contextloader in the next article, if you do not know why this is the entrance to the program, then review the Servletcontextlistener answer Knowledge of the mouth and listener.

ServletContext is used by the Servlet program to communicate with the Web container. such as writing logs, forwarding requests. Each Web application contains a context that is shared by individual programs within the Web application. Because context can be used to save resources and share, the biggest application I know of ServletContext is that the Web cache----to read infrequently changed content into memory, so the server does not need to slow disk I/O when responding to requests.

Servletcontextlistener is a listener for ServletContext, if ServletContext changes, such as when the server is started ServletContext is created, the server shuts down ServletContext will be destroyed.

In the JSP file, application is an instance of ServletContext, created by default by the JSP container. The Getservletcontext () method is called in the Servlet to get an instance of ServletContext.

The idea that we use caching is probably:

1. When the server starts, the Servletcontextlistener contextinitialized () method is called, so the cache is created inside. The cache content generation class can be read from a file or from a database, and the cache class is saved in an instance of ServletContext using the Ervletcontext.setattribute () method.

2. The program uses Servletcontext.getattribute () to read the cache. If it is a JSP, use a pplication.getattribute (). If it is a servlet, use Getservletcontext (). getattribute (). If the cache changes (such as the access count), you can change both the cache and the file/database. Or you wait for changes to accumulate to a certain program to save, you can also save in the next step.

3. When the server is about to close, the Servletcontextlistener contextdestroyed () method is called, so the cached changes are saved inside. Save the changed cache back to the file or database to update the original content.

Java Code

  1. Import User; My own
  2. Classimport Databasemanager; My own class
  3. Import Javax.servlet.ServletContext;
  4. Import Javax.servlet.ServletContextListener;
  5. public class Mycontextlistener implements Servletcontextlistener {
  6. Private ServletContext context = null;
  7. public void contextinitialized (Servletcontextevent event) {
  8. context = Event.getservletcontext ();
  9. User user = Databasemanager.getuserbyid (1);
  10. Context.setattribute ("user1", user);
  11. }
  12. public void contextdestroyed (Servletcontextevent event) {
  13. User user = (user) Context.getattribute ("user1");
  14. Databasemanager.updateuserdata (user);
  15. This.context = null;
  16. }
  17. }

Import User; My own

Classimport Databasemanager; My own class

Import Javax.servlet.ServletContext;

Import Javax.servlet.ServletContextListener;

public class Mycontextlistener implements Servletcontextlistener {

Private ServletContext context = null;

public void contextinitialized (Servletcontextevent event) {

context = Event.getservletcontext ();

User user = Databasemanager.getuserbyid (1);

Context.setattribute ("user1", user);

}

public void contextdestroyed (Servletcontextevent event) {

User user = (user) Context.getattribute ("user1");

Databasemanager.updateuserdata (user);

This.context = null;

}

}

Deployment Servletcontextlistener

You implement (implements) Servletcontextlistener compiled, put it in the correct web-inf/classes directory, change the Web-inf directory of the Web. xml file, add the Web-app node

<listener>

<listener-class>MyServletContextListener</listener-class>

</listener>

If you want to know more, please read this article http://blog.csdn.net/ysughw/article/details/8992322

Do not let reprint, send a link

The Listener Action-contextloaderlistener (RPM) in spring project

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.