Spring is configured in web.xml through contextConfigLocation. The contextConfigLocation
parameter defines the Spring configuration file to be loaded.
If you want to load more than one configuration file, you can <param-value>
use a comma as a separator in the tag.
Configure Listener in web.xml
xml 代码如下:
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener listener-class >
</listener>
If you specify the xml to load for the Listener in web.xml, such as:
The xml code is as follows:
<!-- spring config -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
It will load the corresponding xml instead of applicationContext.xml under /WEB-INF/.
However, if not specified, applicationContext.xml will be loaded under /WEB-INF/ by default.
In an actual project where a team uses Spring, there should be multiple Spring configuration files, how to use and cross-reference:
Multiple configuration files can be written in web.xml separated by spaces, such as:
<CONTEXT-PARAM>
<PARAM-NAME>contextConfigLocation</PARAM-NAME>
<PARAM-VALUE>
applicationContext-database.xml, applicationContext.xml
< /PARAM-VALUE>
</CONTEXT-PARAM>
Cross-references in multiple configuration files can be resolved with ref externals or beans
. For example:
applicationContext.xml
<bean id="userService" class="domain.user.service.impl.UserServiceImpl">
<property name="dbbean">
<ref bean="dbBean"/>
</property>
</bean>
dbBean is in applicationContext-database.xml