一個web工程自動加載的配置文件只有web.xml,想要加載其他.xml必須在web.xml里面進行配置。
用spring的時候需要一個bean容器來管理所有的bean,所有bean默認是寫在applicationContext.xml里的,在web.xml里面是這么設置的,
1 <context-param> 2 <param-name>contextConfigLocation</param-name> 3 <param-value> 4 /WEB-INF/dispatcherServlet-servlet.xml 5 </param-value> 6 </context-param> 7 8 <listener> 9 <listener-class> 10 org.springframework.web.context.ContextLoaderListener 11 </listener-class> 12 </listener>
<context-param>是可選項,如果沒有的話就加載applicationContext.xml,也可以用它指定其他配置文件。
1 <context-param> 2 <param-name>contextConfigLocation</param-name> 3 <param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value> 4 </context-param>
所以,在類的根目錄下寫一個 applicationContext.xml 然后把spring的東西都配置一下 。