简体   繁体   English

Spring无法加载XML配置文件

[英]Spring can't load xml configuration file

This error blows my brain. 这个错误使我震惊。 All the junit tests is fine but when my application loads as plugin for another application I have an errors. 所有的junit测试都很好,但是当我的应用程序作为另一个应用程序的插件加载时,我遇到了错误。 The beans.xml is 100% at root of jar file but for some reason the main application, that loads my plugin can't find the file. beans.xml是jar文件根目录的100%,但是由于某种原因,加载我的插件的主应用程序找不到该文件。 Why? 为什么?

In my application's boot method I call new ClassPathXmlApplicationContext("beans.xml"); 在应用程序的启动方法中,我调用new ClassPathXmlApplicationContext("beans.xml"); throws 抛出

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist

or 要么

new FileSystemXmlApplicationContext(getClass().getClassLoader().getResource("beans.xml").getPath()); throws 抛出

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from URL [file:/C:/path/to/my.jar!/beans.xml]; nested exception is java.io.FileNotFoundException: C:\\path\\to\\my.jar!\\beans.xml (The system cannot find the path specified)


UPD: The problem was the application uses custom classloader that does not load resource files in root, META-INF and etc. but only classes UPD:问题在于应用程序使用自定义类加载器,该类加载器不加载根目录,META-INF等中的资源文件,而仅加载类

The FileNotFoundException that you get really points out that the beans.xml file is not in your jar at the place where you think it is. 您实际上得到的FileNotFoundException指出,beans.xml文件不在您认为存在的位置的jar中。 I would have a look inside that jar. 我去看看那个罐子。 You can for example open the jar with the 7-zip tool . 例如,您可以使用7-zip工具打开罐子。 I find that this provides useful insight when you get errors like the one you report. 我发现当您报告错误时,这提供了有用的见解。

If you're getting the FileNotFoundException it is because the file is not located where you're looking for it. 如果您收到FileNotFoundException那是因为该文件不在您要查找的位置。 The beans.xml file must be located in src/main/resources beans.xml文件必须位于src/main/resources

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM