spring Spring在bean xml配置文件存在时找不到它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12893760/
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-09-08 05:26:16  来源:igfitidea点击:

Spring cannot find bean xml configuration file when it does exist

springconfigurationjavabeansapplicationcontext

提问by dawrutowicz

I am trying to make my first bean in Spring but got a problem with loading a context. I have a configuration XML file of the bean in src/main/resources.

我正在尝试在 Spring 中制作我的第一个 bean,但是在加载上下文时遇到了问题。我在 src/main/resources 中有一个 bean 的配置 XML 文件。

I receive the following IOException:

我收到以下 IOException:

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [src/main/resources/beans.xml]; nested exception is

java.io.FileNotFoundException: class path resource [src/main/resources/beans.xml] cannot be opened because it does not exist

线程“main”中的异常 org.springframework.beans.factory.BeanDefinitionStoreException: IOException 解析来自类路径资源 [src/main/resources/beans.xml] 的 XML 文档;嵌套异常是

java.io.FileNotFoundException: 类路径资源 [src/main/resources/beans.xml] 无法打开,因为它不存在

but I don't get it, since I do the following code test:

但我不明白,因为我做了以下代码测试:

File f = new File("src/main/resources/beans.xml");
System.out.println("Exist test: " + f.exists());

which gives me true! resourcesis in the classpath. What's wrong?

这给了我真实的!resources在类路径中。怎么了?

回答by dawrutowicz

Thanks, but that was not the solution. I found it out why it wasn't working for me.

谢谢,但这不是解决方案。我发现了为什么它对我不起作用。

Since I'd done a declaration:

因为我做了一个声明:

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

I thought I would refer to root directory of the project when beans.xml file was there. Then I put the configuration file to src/main/resources and changed initialization to:

当 beans.xml 文件在那里时,我想我会引用项目的根目录。然后我将配置文件放到 src/main/resources 并将初始化更改为:

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

it still was an IO Exception.

它仍然是一个 IO 异常。

Then the file was left in src/main/resources/ but I changed declaration to:

然后该文件留在 src/main/resources/ 中,但我将声明更改为:

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

and it solved the problem - maybe it will be helpful for someone.

它解决了这个问题——也许它会对某人有所帮助。

thanks and cheers!

感谢和欢呼!

Edit:

编辑:

Since I get many people thumbs up for the solution and had had first experience with Spring as student few years ago, I feel desire to explain shortly why it works.

由于我得到了很多人对这个解决方案的赞许,并且几年前作为学生第一次体验 Spring,我很想简单地解释一下它为什么起作用。

When the project is being compiled and packaged, all the files and subdirs from 'src/main/java' in the project goes to the root directory of the packaged jar (the artifact we want to create). The same rule applies to 'src/main/resources'.

项目在编译打包时,项目中所有来自'src/main/java'的文件和子目录都到打包后的jar(我们要创建的工件)的根目录下。相同的规则适用于“src/main/resources”。

This is a convention respected by many tools like maven or sbt in process of building project (note: as a default configuration!). When code (from the post) was in running mode, it couldn't find nothing like "src/main/resources/beans.xml" due to the fact, that beans.xml was in the root of jar (copied to /beans.xml in created jar/ear/war).

这是许多工具(如 maven 或 sbt)在构建项目过程中遵守的约定(注意:作为默认配置!)。当代码(来自帖子)处于运行模式时,它找不到像“src/main/resources/beans.xml”这样的东西,因为 beans.xml 位于 jar 的根目录中(复制到 /beans .xml 在创建的 jar/ear/war 中)。

When using ClassPathXmlApplicationContext, the proper location declaration for beans xml definitions, in this case, was "/beans.xml", since this is path where it belongs in jar and later on in classpath.

当使用 ClassPathXmlApplicationContext 时,bean xml 定义的正确位置声明,在这种情况下,是“/beans.xml”,因为这是它在 jar 中所属的路径,然后在类路径中。

It can be verified by unpacking a jar with an archiver (i.e. rar) and see its content with the directories structure.

可以通过使用存档器(即 rar)解压缩 jar 并使用目录结构查看其内容来验证它。

I would recommend reading articles about classpath as supplementary.

我建议阅读有关类路径的文章作为补充。

回答by Mikhail Kolesnikov

Try this:

尝试这个:

new ClassPathXmlApplicationContext("file:src/main/resources/beans.xml");

new ClassPathXmlApplicationContext("file:src/main/resources/beans.xml");

file:preffix point to file system resources, not classpath.

file:指向文件系统资源的前缀,而不是类路径。

file path can be relative or system (/home/user/Work/src...)

文件路径可以是相对路径或系统路径(/home/user/Work/src...)

回答by Kalher

I also had a similar problem but because of a bit different cause so sharing here in case it can help anybody.

我也有类似的问题,但由于原因有点不同,所以在这里分享,以防它可以帮助任何人。

My file location

我的文件位置

beans.xml file

beans.xml 文件

How I was using

我是如何使用的

ClassPathXmlApplicationContext("beans.xml");

ClassPathXmlApplicationContext("beans.xml");

There are two solutions

有两种解决方案

  1. Take the beans.xml out of package and put in default package.
  2. Specify package name while using it viz.
  1. 将 beans.xml 从包中取出并放入默认包中。
  2. 使用时指定包名,即。

ClassPathXmlApplicationContext("com/mypackage/beans.xml");

ClassPathXmlApplicationContext("com/mypackage/beans.xml");

回答by Gani

This is because applicationContect.xml or any_filename.XML is not placed under proper path.

这是因为 applicationContect.xml 或 any_filename.XML 未放置在正确的路径下。

Trouble shooting Steps

故障排除步骤

1: Add the XML file under the resource folder.

1:在资源文件夹下添加xml文件。

2: If you don't have a resource folder. Create one by navigating new by Right click on the project new > Source Folder, name it as resourceand place your XML file under it.

2:如果你没有资源文件夹。右键单击新建项目 > 源文件夹,将其命名为资源并将您的 XML 文件放在其下。

回答by OrangeDog

src/main/resourcesis a source directory, you should not be referencing it directly. When you build/package the project the contents will be copied into the correct place for your classpath. You should then load it like this

src/main/resources是一个源目录,你不应该直接引用它。当您构建/打包项目时,内容将被复制到您的类路径的正确位置。然后你应该像这样加载它

new ClassPathXmlApplicationContext("beans.xml")

Or like this

或者像这样

new GenericXmlApplicationContext("classpath:beans.xml");

回答by Deep Shah

use it ApplicationContext context = new FileSystemXmlApplicationContext("Beans.xml");

用它 ApplicationContext context = new FileSystemXmlApplicationContext("Beans.xml");

回答by Brian Agnew

I suspect you're building a .war/.jar and consequently it's no longer a file, but a resource within that package. Try ClassLoader.getResourceAsStream(String path)instead.

我怀疑您正在构建一个 .war/.jar,因此它不再是一个文件,而是该包中的一个资源。尝试ClassLoader.getResourceAsStream(String path)代替。

回答by Michael Z

You have looked at src directory. The xml file indeed exist there. But look at class or bin/build directory where all your output classes are set. I suspect you will need only resources/beans.xml path to use.

您已经查看了 src 目录。xml 文件确实存在于那里。但是请查看设置所有输出类的 class 或 bin/build 目录。我怀疑您只需要使用 resources/beans.xml 路径。

回答by Kanagavelu Sugumar

Note that the first applicationContext is loaded as part of web.xml; which is mentioned with the below.

请注意,第一个 applicationContext 是作为web.xml; 的一部分加载的。这是在下面提到的。

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>META-INF/spring/applicationContext.xml</param-value>
</context-param>

<servlet>
    <servlet-name>myOwn-controller</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>META-INF/spring/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

Where as below code will also tries to create one more applicationContext.

下面的代码还将尝试创建一个更多的 applicationContext。

private static final ApplicationContext context = 
               new ClassPathXmlApplicationContext("beans.xml");

See the differencebetween beans.xmland applicationContext.xml

差异之间beans.xmlapplicationContext.xml

And if appliationContext.xmlunder <META-INF/spring/>has declared with <import resource="beans.xml"/>then this appliationContext.xmlis loading the beans.xmlunder the same location META-INF/springof appliationContext.xml.

如果appliationContext.xml<META-INF/spring/>已经声明了<import resource="beans.xml"/>,然后这个appliationContext.xml被加载beans.xml的同一位置下META-INF/springappliationContext.xml

Where as; in the code; if it is declared like below

然而; 在代码中;如果它声明如下

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

This is looking the beans.xml at WEB-INF/classesOR in eclipse src/main/resources.

这是WEB-INF/classes在 eclipse 中查看 beans.xml 中的OR src/main/resources

[If you have added beans.xmlat src/main/resourcesthen it might be placed at WEB-INF/classeswhile creating the WAR.]

[如果您添加了beans.xmlatsrc/main/resources那么它可能会在WEB-INF/classes创建 WAR 时放置。]

So totally TWOfiles are looked up.

所以总共查找了两个文件。

I have resolved this issue by adding classpath lookup while importing at applicationContext.xmllike below

我通过在导入时添加类路径查找解决了这个问题,applicationContext.xml如下所示

<import resource="classpath*:beans.xml" />

and removed the the line ClassPathXmlApplicationContext("beans.xml")in java code, so that there will be only one ApplicationContext loaded.

并删除了ClassPathXmlApplicationContext("beans.xml")java代码中的那一行,这样只会加载一个ApplicationContext。

回答by Shashesh

In Spring all source files are inside src/main/java. Similarly, the resources are generally kept inside src/main/resources. So keep your spring configuration file inside resources folder.

在 Spring 中,所有源文件都在 src/main/java 中。同样,资源一般保存在 src/main/resources 中。因此,请将您的 spring 配置文件保存在资源文件夹中。

Make sure you have the ClassPath entry for your files inside src/main/resources as well.

确保在 src/main/resources 中也有文件的 ClassPath 条目。

In .classpath check for the following 2 lines. If they are missing add them.

在 .classpath 检查以下 2 行。如果缺少它们,请添加它们。

<classpathentry path="src/main/java" kind="src"/>
<classpathentry path="src/main/resources" kind="src" />

So, if you have everything in place the below code should work.

因此,如果您已准备好所有内容,则下面的代码应该可以工作。

ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring-Module.xml");

ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring-Module.xml");