Spring configuration in web. xml: springweb. xml
Spring configuration in web. xml
Preface
In actual projects, the spring configuration file applicationcontext. xml is automatically loaded to the container through the loading mechanism provided by spring. In a web project, the configuration file is loaded into the web container for parsing. Currently, spring provides two loaders for loading web containers:One is ContextLoaderListener and the other is ContextLoaderServlet.. The two functions are identical, but the former is implemented based on the Listener interface introduced in Servlet2.3, and the latter is implemented based on the Servlet interface, the following are the two loaders on the web. configuration application in xml:
ContextLoaderListener
<listener> <listener-class>org.springframework.context.ContextLoaderListener</listener-class> </listener>
ContextLoaderServlet
<servlet> <servlet-name>context</servlet-name> <servlet-class>org.springframework.context.ContextLoaderServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
With the preceding configuration, the web Container automatically loads applicationcontext. xml for initialization.
To specify the location of the configuration file, you can use context-param to specify the location:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/myApplicationContext.xml</param-value> </context-param>
Then, you can use the WebApplicationContextUtils. getWebApplicationContext method to obtain the reference of applicationcontext in the web application.
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!