Three ways to configure spring.xml in web.xml

We know that spring can configure its xml path in three ways in web.xml:
org.springframework.web.servlet.DispatcherServlet
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.ContextLoaderServlet
When using the first one When configured in this way, the WebApplicationContext loaded by spring belongs to this servlet (so spring can configure multiple DispatcherServlets with their own environments), so other servlets cannot obtain the Context. This phenomenon has occurred in buffalo configuration (cannot find service bean). After analyzing the source code of buffalo and spring, the xml configuration in ContextLoaderListener can be solved.

org.springframework.web.servlet.DispatcherServlet
is the Front Controller when using Spring MVC, and does not initialize the applicationContext.
Just apply it, and definitely won't find the bean definition.

The web program must use either org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.ContextLoaderServlet
to initialize the container

 

The role of the ContextLoaderListener 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 HttpServlet . this 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 the xml file of applicationContext. If you do not write any parameter configuration information in web.xml, the default path is "/WEB-INF/applicationContext.xml, and the xml file created in the WEB-INF directory is The name must be applicationContext.xml. If you want to customize the file name, you can add the context parameter of 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 separated by a ",". 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.

 
 
http://www.cnblogs.com/sos-blue/archive/2013/10/11/3362589.html
http://blog.csdn.net/zheng963/article/details/42149779

Guess you like

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