SSH集成org.springframework.beans.factory.BeanDefinitionStoreException: IOException

时间:2021-02-05 20:33:34
系统环境:JDK8+Eclipse4.4+Tomcat7.x+Spring4.1.3+Hibernate4.3.7+Struts2.3.20,我的配置文件均存放在src目录下。
首先Spring默认配置文件是applicationContext.xml,如果不叫这个名,那就需要额外指定了,指定方式是在web.xml文件中加入:
<param-name>contextConfigLocation</param-name>和<param-value>classpath:beans.xml,classpath:action-beans.xml</param-value>,但是遗憾的是,tomcat一启动就报类似如下的错误:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/beans.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/beans.xml]  
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/beans.xml] 

最后经过不断的测试,调整,得到了如下结论,仅供参考:
<!-- needed for ContextLoaderListener -->
 <!-- 指定spring配置文件的位置 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <!-- 可以成功加载配置文件 -->
  <!-- <param-value>/WEB-INF/classes/beans.xml,/WEB-INF/classes/action-beans.xml</param-value> -->
  <!-- 不能成功加载配置文件 -->
  <!-- <param-value>/WEB-INF/beans.xml,/WEB-INF/action-beans.xml</param-value> -->
  <!-- 可以成功加载配置文件 -->
  <!-- <param-value>classpath:*beans.xml</param-value> -->
  <!-- 不能成功加载配置文件 -->
  <!-- <param-value>classpath:beans.xml,action-beans.xml</param-value> -->
  <!-- 可以成功加载配置文件 -->
  <param-value>classpath:beans.xml,classpath:action-beans.xml</param-value>
 </context-param>
 <!-- Bootstraps the root web application context before servlet initialization -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
其实我懂的不多,但是我相信*,我相信老外啊,看这里有一句话,是这么说的
Do not use classpath. This may cause problems with different ClassLoaders (container vs. application). WEB-INF is always the better choice.
不要使用classpath,当类的加载器不同的时候可能会出问题,建议使用WEB-INF。
那么最佳的配置就是在src的同级目录下,新建一个source folder文件夹,然后把所有的配置文件纳入其中,之后在web.xml中,使用WEB-INF/classes/beans.xml进行配置,多个文件之间以逗号分隔。

最后上图:
SSH集成org.springframework.beans.factory.BeanDefinitionStoreException: IOException