Spring container startup
Spring container startup.
I also read the source code by myself, so that I can easily remember and understand it later. Therefore, I am writing this article. If there is anything wrong, please submit it.
For tomcat-based server projects, we first look at the web. xml file. The spring container starts to load listeners.
See the following code: its listener uses the ContextLoaderListener class in spring api.
ContextConfigLocation
Classpath *:/spring-*. xml
Spring. profiles. default
Production
Org. szgzw. frame. web. CGYContextLoaderListener
Observe that we have written a listener class CGYContextLoaderListener,
Let's first understand the ContextLoaderListener class in spring api.
The function of the ContextLoaderListener listener is to automatically assemble the configuration information of ApplicationContext when the Web container is started. 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. There is no detailed description on how to configure multiple xml files when the configuration file ApplicationContext. xml is deployed. The current method is to view its API documentation. ContextLoader is associated with ContextLoader in ContextLoaderListener. Therefore, ContextLoader completes the configuration loading process. Let's take a look at its API description.
The first section describes that ContextLoader can be generated by ContextLoaderListener and ContextLoaderServlet. If you view the ContextLoaderServlet API, you can see that it is also associated with the ContextLoader class and it implements the HttpServlet interface.
In the second section, ContextLoader creates a class such as XmlWebApplicationContext, which implements the WebApplicationContext-> ConfigurableWebApplicationContex> ApplicationContext-> BeanFactory. In this way, all beans in spring are created by this class.
Section 3 describes how to deploy the xml file of applicationContext.
If no parameter configuration information is written in web. xml, the default path is/WEB-INF/applicationContext. xml, and the name of the xml file created under the WEB-INF directory must be applicationContext. xml;
If you want to customize the file name, you can add the contextConfigLocation context parameter in web. xml:
ContextConfigLocation
/WEB-INF/classes/applicationContext-*. xml
In Specify the corresponding xml file name. If there are multiple xml files, you can write them together and separate them with a comma. The above applicationContext-*. xml uses wildcards, such as the directory under the applicationContext-ibatis-base.xml, applicationContext-action.xml, applicationContext-ibatis-dao.xml and other files, will be loaded together.
It can be seen that the file location of applicationContext. xml has two default implementations:
First: put it directly under the/WEB-INF, in the web. xml declare a listener;
Type 2: Put it under classpath, but add it to web. xml To specify the location of your applicationContext. xml for the web container to load. According to the official file provided by Struts2 integrated with spring, it is written:
ContextConfigLocation
/WEB-INF/applicationContext-*. xml, classpath *: applicationContext-*. xml
In this way, we can see the class CGYContextLoaderListener, and we know that it certainly implements the interface servlet api ServletContextListener, or we know that it inherits the class spring api ContextLoaderListener.
At the underlying layer, the interface servlet api ServletContextListener inherits the tag interface that must be extended by all the event listening interfaces of the interface java. util. EventListener,
ServletContext: Each web application has a ServletContext associated with it. The ServletContext object is created when the application is started and destroyed when the application is closed. ServletContext is valid globally, similar to a global variable in an application.
ServletContextListener: using the listener interface, developers can add arbitrary objects to ServletContext before providing services for client requests. This object is initialized when ServletContext is started, and then visible throughout the runtime of ServletContext. This interface has two methods:
Servletcontextlistener code
- Void contextDestoryd (ServletContextEvent sce );
-
- Void contextInitialized (ServletContextEvent sce );
You need to create a java class to implement the javax. servlet. ServletContextListener interface and provide the implementation of the above two methods.
Example: When you need to create a database connection before processing any client request and want the connection to be available throughout the application process, the ServletContextListener interface will be very useful.
Java code
- Package com. database;
- Import javax. servlet. ServletContext;
- Import javax. servlet. ServletContextAttributeEvent;
- Import javax. servlet. ServletContextAttributesListener;
- Import javax. servlet. ServletContextEvent;
- Import javax. servlet. ServletContextListener;
- Import com. database. DbConnection;
-
- Public class DatabaseContextListener implements ServletContextListener {
-
- Private ServletContext context = null;
- Private Connection conn = null;
-
- Public DatabaseContextListener (){
-
- }
- // This method is called after ServletContext is started and ready to process client requests
- Public void contextInitialized (ServletContextEvent event ){
- This. context = event. getServletContext ();
- Conn = DbConnection. getConnection;
- // Here DbConnection is a custom class used to create a database connection
- Context = setAttribute ("dbConn", conn );
- }
-
- // This method is called when ServletContext is about to be closed
- Public void contextDestroyed (ServletContextEvent event ){
- This. context = null;
- This. conn = null;
- }
- }
Deploy the class and add it to the web. xml file.
Xml Code
-
- Com. database. DatabaseContextListener
Once the web application is started, we can obtain the database connection in any servlet or jsp in the following way:
Java code
- Connection conn = (Connection) getServletContext (). getAttribute ("dbConn"); here we will think of the difference between the two Ghost servletcontext and servletcontextlistener. Through the code above, it seems that we initialize the interface servletcontextlistener at startup, at the same time, we can implement two methods of servletcontextlistener. In these two methods, we can get servletcontext, since we have obtained servletcontext. Therefore, when a WEB container is started, it creates a corresponding ServletContext object for each WEB application, which represents the current web application. Is a global storage space servletContext, which is shared by all users. Therefore, in order to save space and improve efficiency, it is safe to put necessary, important threads that all users need to share in ServletContext.