在web.xml中通過contextConfigLocation配置spring,contextConfigLocation參數定義了要裝入的 Spring 配置文件。
1. 在web.xml里配置需要加載的spring配置文件。
如果要裝入多個配置文件,在<param-value>標記中用逗號作分隔符即可。
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-base.xml,classpath:spring-another.xml</param-value> </context-param>
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring-base.xml,classpath:spring-another.xml</param-value> </context-param>
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:**/spring-*.xml</param-value> </context-param>
2. 在web.xml里配置Listener,本Listener會去加載第一步中指定的配置文件。
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
3. 如果指定了要加載的文件,則會去加載相應的xml,而不會去加載/WEB-INF/下的applicationContext.xml。(指定加載即是第1步的配置)
如果沒有指定的話,默認會去/WEB-INF/下加載applicationContext.xml。
web.xml
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 可以自定義servlet.xml配置文件的位置和名稱,默認為WEB-INF目錄下,名稱為[<servlet-name>]-servlet.xml,如spring-servlet.xml <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> 默認 </init-param> --> <load-on-startup>1</load-on-startup> </servlet>
4. 多個配置文件里的交叉引用可以用ref的external或bean解決
<bean id="userService" class="domain.user.service.impl.UserServiceImpl"> <property name="dbbean"> <ref bean="dbBean"/> </property> </bean>
轉 : https://blog.csdn.net/vicroad2014/article/details/82384370
有個簡單的spring-mvc例子:
https://www.cnblogs.com/fps2tao/p/7269369.html
https://www.cnblogs.com/fps2tao/p/7274173.html