Java 无法打开 beans.xml(配置文件),因为不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21479331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:55:32  来源:igfitidea点击:

Can not open beans.xml (config file) because does not exist

javaxmlspringjavabeans

提问by Joe

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

线程“main”中的异常 org.springframework.beans.factory.BeanDefinitionStoreException: IOException 解析来自类路径资源 [com/main/beans.xml] 的 XML 文档;嵌套异常是 java.io.FileNotFoundException: 类路径资源 [com/main/beans.xml] 无法打开,因为它不存在

ApplicationContext context = 
      new ClassPathXmlApplicationContext("com/main/beans.xml");

I have tried before with

我以前试过

ApplicationContext context = 
     new FileSystemXmlApplicationContext("src/main/java/com/main/beans.xml");

And it works well.

它运作良好。

How to do that relative to the classpath?

相对于类路径如何做到这一点?

Note:classpath is in the build path

注意:类路径在构建路径中



In the example I'm following, it has the following structure and it works

在我正在关注的示例中,它具有以下结构并且可以正常工作

Project structure

项目结构

Project structure

项目结构

Classpath

类路径

Classpath

类路径

ApplicationContext context = 
    new ClassPathXmlApplicationContext("com/caveofprogramming/spring/test/beans/beans.xml");

回答by Paul Samsotha

Here's the file structure I normally use, which works fine. As @M.Deinum said, you'll want to put your xml file in a src/main/resources. I normally put to the it in a complete package path with the resources so during compile time, maven will add all the resources to same path as the corresponding classes that use them.

这是我通常使用的文件结构,效果很好。正如@M.Deinum 所说,您需要将 xml 文件放在src/main/resources. 我通常将它与资源放在一个完整的包路径中,因此在编译期间,maven 会将所有资源添加到与使用它们的相应类相同的路径中。

enter image description here

在此处输入图片说明

resources get copied to the class package when you do the above

执行上述操作时,资源会被复制到类包中

enter image description here

在此处输入图片说明

public class App {

    public static void main(String[] args) {
        ApplicationContext context
                = new ClassPathXmlApplicationContext("com/underdogdevs/stackmaven/beans.xml");

        Hello hello = (Hello) context.getBean("hello");
        hello.sayHello();
    }
}

Works fine for me. If you're wondering why you still need to use the complete package name when the xml is already in the same class packages, its it will first be searched for in the class root

对我来说很好用。如果你想知道为什么当 xml 已经在同一个类包中时你仍然需要使用完整的包名,它会首先在类根中搜索



UPDATE

更新

put the package with the bean.xmlinto the src/main/resources. It should work with the path your using.

将带有 的包bean.xml放入src/main/resources. 它应该适用于您使用的路径。



UPDATE 2

更新 2

"Yes, it worked. But why is it working the example, I'm following as well. If the beans.xml is out of src/main/resources .. I can't find out how that works? *

“是的,它有效。但为什么它在这个例子中起作用,我也在关注。如果 beans.xml 不在 src/main/resources .. 我不知道它是如何工作的?*

The thing is, the Spring container will look from the class root. It has nothing to with the resourcesfolder. The resourcesis a convenience dir for maven projects to build to your class path. The reason the tutorial works, is that the beans.xmlis in a package, that will get put into the class path in the build, as seen below. It is only preferredto use a resources, but a package` will also build to the class path.

问题是,Spring 容器将从类根中查看。它与resources文件夹无关。这resources是 Maven 项目构建到您的类路径的便利目录。本教程有效的原因是它beans.xml在一个包中,它将被放入构建中的类路径中,如下所示。仅首选使用 a resources,但 package` 也将构建到类路径。

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明