Detailed explanation of spring's ContextLoaderListener usage

The role of the ContextLoaderListener listener is to automatically assemble the configuration information of the ApplicationContext when the Web container is started. Because it implements the ServletContextListener interface, the listener is configured in web.xml and the method it implements will be executed by default when the container is started. As for where the configuration file ApplicationContext.xml is deployed, how to configure multiple xml files is not explained in detail in the book. The way to go now is to look at its API documentation. The ContextLoader class is associated with the ContextLoaderListener, so the entire loading configuration process is completed by the ContextLoader. Take a look at its API description.

The first paragraph states that ContextLoader can be generated by ContextLoaderListener and ContextLoaderServlet. If you look at the API of ContextLoaderServlet, you can see that it is also associated with the ContextLoader class and it implements the HttpServlet interface.

 In the second paragraph, ContextLoader creates a class like XmlWebApplicationContext, which implements the interface WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->BeanFactory so that all beans in spring

are created by this class. The third paragraph describes how to deploy 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 xml file created in the WEB-INF directory must be applicationContext.xml;

If you want to customize the file name, you can add the context parameter contextConfigLocation in web.xml:
<context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>  
            /WEB-INF/classes/applicationContext- *.xml   
        </param-value>  
    </context-param>  
Specify the corresponding xml file name in <param-value> </param-value>, if there are multiple xml files, they can be written together and combined", ” separated. The above applicationContext-*.xml uses wildcards. For example, there are applicationContext-ibatis-base.xml, applicationContext-action.xml, applicationContext-ibatis-dao.xml and other files in this directory, which will be loaded together.

It can be seen that the file location of applicationContext.xml can have two default implementations:

the first: put it directly under /WEB-INF, and declare a listener in web.xml;

the second: put it in classpath, but add <context-param> to web.xml at this time, and use it to indicate your applicationContext. xml location for the web container to load.
According to the official file given by Struts2 to integrate spring, write: <context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> 
</context-param>

Or context-param can be written like this

  <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath*:spring/*.xml</param-value>

  </context-param>

  <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326774495&siteId=291194637