Spring container startup

Source: Internet
Author: User

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.

Classpath *:/spring-*. xml

Spring. profiles. default

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:


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

/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
  1. Void contextDestoryd (ServletContextEvent sce );
  3. 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
    1. Package com. database;
    2. Import javax. servlet. ServletContext;
    3. Import javax. servlet. ServletContextAttributeEvent;
    4. Import javax. servlet. ServletContextAttributesListener;
    5. Import javax. servlet. ServletContextEvent;
    6. Import javax. servlet. ServletContextListener;
    7. Import com. database. DbConnection;
    9. Public class DatabaseContextListener implements ServletContextListener {
    11. Private ServletContext context = null;
    12. Private Connection conn = null;
    14. Public DatabaseContextListener (){
    16. }
    17. // This method is called after ServletContext is started and ready to process client requests
    18. Public void contextInitialized (ServletContextEvent event ){
    19. This. context = event. getServletContext ();
    20. Conn = DbConnection. getConnection;
    21. // Here DbConnection is a custom class used to create a database connection
    22. Context = setAttribute ("dbConn", conn );
    23. }
    25. // This method is called when ServletContext is about to be closed
    26. Public void contextDestroyed (ServletContextEvent event ){
    27. This. context = null;
    28. This. conn = null;
    29. }
    30. }

      Deploy the class and add it to the web. xml file.

      Xml Code
      1. Com. database. DatabaseContextListener
      2. Once the web application is started, we can obtain the database connection in any servlet or jsp in the following way:

        Java code
        1. 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.



Related Article

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.