Listener function in the spring Project-contextloaderlistener (when the project is started, something is loaded into the cache)

Source: Internet
Author: User

2. automatically assemble the configuration information of spring applicationcontext. xml when starting a Web container.

Because it implements the servletcontextlistener interface, the listener is configured in Web. XML, and the implementation method is executed by default when the container is started. Contextloader is associated with the contextloader class in contextloaderlistener. Therefore, contextloader completes the configuration loading process.

 

The Pring entry under the Web is in the listener of the configuration 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, it must implement the contextdestroyed and contextinitialized methods.

Publicclass contextloaderlistener implements
Servletcontextlistener {

Privatecontextloader contextloader;

/**

* Initialize the root web application context.

*/

// Start the Spring framework. contextinitialized is also the main entry function of the listener class.

Publicvoid contextinitialized (servletcontextevent event ){

This. contextloader = createcontextloader ();

This. contextloader. initwebapplicationcontext (event. getservletcontext ());

}

/**

* Createthe contextloader to use. can be overridden in subclasses.

* @ Returnthe new contextloader

*/

Protectedcontextloader createcontextloader (){

Return new contextloader ();

}

/**

* Returnthe contextloader used by this listener.

* @ Returnthe current contextloader

*/

Publiccontextloader getcontextloader (){

Return this. contextloader;

}

/**

* Closethe root web application context.

*/

Publicvoid contextdestroyed (servletcontextevent event ){

If (this. contextloader! = NULL ){

This. contextloader. closewebapplicationcontext (event. getservletcontext ());

}

}

}

 

In general, this entry is very simple, and all implementations are hidden in the contextloader class. We will discuss contextloader in the next article. If you do not know why it is the entry of the program, let's review the knowledge about the servletcontextlistener interface and listener.

Servletcontext is used by Servlet programs to communicate with Web containers. For example, write logs and forward requests. Each web application contains a context, which is shared by various programs in the Web application. Because context can be used to save and share resources, the biggest application of servletcontext I know is Web Cache-reading infrequently changed content into the memory, therefore, when the server responds to the request, it does not need to perform slow disk I/O.

Servletcontextlistener is the listener of servletcontext. If servletcontext changes, for example, servletcontext is created when the server is started, servletcontext will be destroyed when the server is closed.

In the JSP file, the application is an instance of servletcontext, which is created by default by the JSP Container. Call the getservletcontext () method in servlet to obtain the instance of servletcontext.

The following describes how to use cache:

1. When the server is started, the contextinitialized () method of servletcontextlistener is called, so the cache is created. You can read the cache content from a file or from a database to generate a class. Use the ervletcontext. setattribute () method to save the cache class to the instance of servletcontext.

2. The program uses servletcontext. getattribute () to read the cache. For JSP, use a pplication. getattribute (). For servlet, use getservletcontext (). getattribute (). If the cache changes (such as the access count), you can change the cache and file/database at the same time. Or you can save the changes after accumulating a certain number of programs, or you can save them in the next step.

3. When the server is about to be shut down, the contextdestroyed () method of servletcontextlistener is called, so the cached changes are saved in it. Save the changed cache to a 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

Importjavax. servlet. servletcontext;

Importjavax. servlet. servletcontextlistener;

Public classmycontextlistener implementsservletcontextlistener {

Private servletcontext context = NULL;

Public voidcontextinitialized (servletcontextevent event ){

Context = event. getservletcontext ();

User user = databasemanager. getuserbyid (1 );

Context. setattribute ("user1", user );

}

Public voidcontextdestroyed (servletcontextevent event ){

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

Databasemanager. updateuserdata (User );

This. Context = NULL;

}

}

 

Deploy servletcontextlistener

After you compile servletcontextlistener, place it in the correct WEB-INF/classes directory, change the Web. xml file under the WEB-INF directory, and add

<Listener>

<Listener-class> myservletcontextlistener </listener-class>

</Listener>

-------------------------------------- Obtain data under different circumstances -----------------------------------------

In the spring task Timer class:

Implement the applicationcontextaware interface public class A implements servletcontextaware {
Private servletcontext;

@ Override
Public void setservletcontext (servletcontext ){
This. servletcontext = servletcontext;
}

Public void B (){
Servletcontext. getattribute ("uid ")
}}

Controller class:
Servletcontext = request. getsession (). getservletcontext ();
Servletcontext. getattribute ("uid ");

Java common class:

Servletcontext can only be transmitted from the Controller or modeled as the task class.

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.