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