My view of spring container launch.
I also look at the source code, and then facilitate their own memory and understanding, so write this article, if there is anything wrong place also ask you to put forward.
For Tomcat to do the server project, we first look at theis Web. xml file, the spring container starts to load the listener
Look at the following code: its listener uses the class Contextloaderlistener in the Spring API
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value> classpath*:/spring-*.xml</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>production</param-value>
</context-param>
<listener>
<listener-class>org.szgzw.frame.web.CGYContextLoaderListener</listener-class>
<!--<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>-- >
</listener>
Observe the above found here we wrote a listening class ourselvesCgycontextloaderlistener,
Let's start by understanding the Contextloaderlistener class in the Spring API
The role of the Contextloaderlistener listener is to automatically assemble ApplicationContext 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. As for applicationcontext.xml where this configuration file is deployed, how to configure multiple XML files is not explained in detail in the book. The way to do this now is to look at its API documentation. The Contextloader class is associated with Contextloaderlistener, so the entire load configuration process is done by Contextloader. Take a look at its API description.
The first paragraph shows that contextloader can be generated by Contextloaderlistener and Contextloaderservlet. If you look at the Contextloaderservlet API, you can see that it also associates the Contextloader class and it implements the HttpServlet interface.
In the second paragraph, Contextloader creates a class Xmlwebapplicationcontext that implements an interface that is webapplicationcontext-> Configurablewebapplicationcontex>applicationcontext->beanfactory so that all the beans in spring are created by this class
The third paragraph, how to deploy the ApplicationContext XML file.
If you do not write any parameter configuration information in Web. XML, the default path is/web-inf/applicationcontext.xml, and the name of the file created under the Web-inf directory must be applicationcontext.xml;
If you want to customize the file name, you can add contextconfiglocation to the context parameter in Web. XML:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/web-inf/classes/applicationcontext-*.xml
</param-value>
</context-param>
In <param-value> </param-value> Specify the appropriate XML file name, if there are multiple XML files, can be written together and one "," number separated. The applicationcontext-*.xml above uses a wildcard character, For example, in this directory there are applicationcontext-ibatis-base.xml,applicationcontext-action.xml,applicationcontext-ibatis-dao.xml and other files, will be loaded together.
This shows that the Applicationcontext.xml file location can have two default implementations:
The first kind: put it directly under/web-inf, and declare a listener in Web. xml;
The second kind: put it under Classpath, but add <context-param> to the Web. XML, and use it to indicate the location of your applicationcontext.xml to be loaded by the container. According to Struts2, the official files of spring are written as follows:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
</context-param>
so we see the Cgycontextloaderlistener class, and we know that he must have also implemented the interface Servlet API Servletcontextlistener, or we know it inherits the class spring API Contextloaderlistener
Then look at the bottom, interface servlet API Servletcontextlistener inherits the interface Java.util.EventListener EventListener all event listener interfaces must be extended by the tag interface,
ServletContext: Every web App has a servletcontext associated with it. The ServletContext object is created when the app is launched and is destroyed when the app is closed. The 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 serving client requests. This object is initialized when the ServletContext is started, and is visible throughout the ServletContext operation. The interface has two methods as shown below:
Servletcontextlistener Code
- void Contextdestoryd (Servletcontextevent sce);
- void contextinitialized (Servletcontextevent sce);
The user needs to create a Java class to implement the Javax.servlet.ServletContextListener interface and provide implementations of the above two methods.
Example: The Servletcontextlistener interface is useful when you need to create a database connection before processing any client requests, and you want the connection to be available throughout the application.
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 handle client requests
- Public void contextinitialized (servletcontextevent event) {
- This . Context = Event.getservletcontext ();
- conn = dbconnection.getconnection;
- //Here DbConnection is a custom class to create a database connection
- Context = SetAttribute ("Dbconn", conn);
- }
- //This method will be called when ServletContext is about to close
- Public void contextdestroyed (servletcontextevent event) {
- This . Context = NULL ;
- This . conn = NULL ;
- }
- }
The class is then deployed and added in the Web. xml file
XML code
- < Listener >
- Com.database.DatabaseContextListener
- </ Listener >
Once the Web application starts, we can get the database connection in any servlet or JSP in the following way:
Java code
- Connection conn = (Connection) getservletcontext (). getattribute ("Dbconn");
here, we'll think of the difference between these two ghosts ServletContext and Servletcontextlistener .By the above code, I find it seems as if we were going to initialize the interface Servletcontextlistener while booting, and implement the Servletcontextlistener two methods, in which we can get theServletContextNow that we have acquired the ServletContext. ThusWEBwhen the container is started, it will beWEBthe application creates a corresponding servletcontext Object ,it represents the currentWeb Application. is a global space for storing information ServletContext, which is shared by all users. Therefore, in order to save space and improve efficiency, in servletcontext, it is necessary to put the required, important, all users need to share the thread is a security of some information.
My view on the Spring container initiation