未随机找到类路径资源
我们在 Eclipse 中将 Spring 与 Hibernate 和 Struts2 集成,并在运行构建时遇到这些错误:
14-Oct-2011 10:37:27 org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [Test.hbm.xml] cannot be opened because it does not exist
...
Caused by: java.io.FileNotFoundException: class path resource [Test.hbm.xml] cannot be opened because it does not exist
...
我们在 applicationContext.xml 中的映射是:
<property name="mappingResources" value="Test.hbm.xml" />
Test.hbm.xml 位于 /build/classes ,我们尝试将其放入/WEB-INF、/src,甚至创建了一个名为/hibernate的新源文件夹> 然后把它扔进去只在几台机器上工作。
我们第一次写的时候,就出现了这个错误。然后我们将完全相同的代码复制到一个新项目中,它突然起作用了。在某些机器上,它可以工作;在其他人上,它显示此错误。
有谁知道可能出了什么问题?如果需要任何其他信息,请告诉我。
非常感谢。
We have integrated Spring with Hibernate and Struts2 in Eclipse and have been getting these errors when running the build:
14-Oct-2011 10:37:27 org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [Test.hbm.xml] cannot be opened because it does not exist
...
Caused by: java.io.FileNotFoundException: class path resource [Test.hbm.xml] cannot be opened because it does not exist
...
Our mapping in applicationContext.xml is:
<property name="mappingResources" value="Test.hbm.xml" />
Test.hbm.xml is in /build/classes, and we have tried putting it in /WEB-INF, /src, and even created a new source folder called /hibernate and dumped it in and it worked on only a few machines.
The first time we wrote it, it showed this error. And then we copied the exact same code into a new project, it suddenly worked. On some machines, it works; on others, it shows this error.
Does anyone know what could be wrong? Let me know if any other information is needed.
Thanks so much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是构建时错误。这是一个运行时的。不过,这种间歇性的失败表明您的构建存在问题。作为类路径资源,当您运行应用程序时,Test.hbm.xml 必须位于类路径位置。具体在哪里取决于你如何运行它,但 /WEB-INF 永远不会为 web 应用程序工作,并且随机的 /hibernate 文件夹也不太可能工作,除非你已经完成了一些自定义配置以使其成为类路径资源或将其构建为一个。为了获得更明确的答案,您需要提供有关您正在运行的内容以及如何构建它的更多详细信息。
That's not a build-time error. It's a run-time one. Intermittent failures of that sort indicate a problem with your build, though. As a classpath resource, Test.hbm.xml must be in a classpath location when you run your app. Where exactly that is depends on how you're running it, but /WEB-INF will never work for a webapp, and a random /hibernate folder is unlikely to work, too, unless you've done some custom configuration to make that a classpath resource or make it build to one. For a more definitive answer, you'll need to provide more detail about what you're running and how you're building it.