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
- 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;
- }
- }
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.