Spring 找不到 bean xml 配置文件,当它确实存在时 [英] Spring cannot find bean xml configuration file when it does exist

查看:145
本文介绍了Spring 找不到 bean xml 配置文件,当它确实存在时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我收到以下 IOException:

I receive the following IOException:

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

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: 类路径资源 [src/main/resources/beans.xml] 不能被打开,因为它不存在

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

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

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());

这让我很真实!resources 在类路径中.怎么了?

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

推荐答案

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

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

因为我做了一个声明:

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

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

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:

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

它仍然是一个 IO 异常.

it still was an IO Exception.

然后文件被留在 src/main/resources/但我改变了声明:

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

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

它解决了问题 - 也许它会对某人有所帮助.

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

感谢和欢呼!

由于我的解决方案得到很多人的赞许,并且几年前我作为学生第一次体验 Spring,因此我很想简单地解释一下它为什么有效.

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.

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

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'.

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

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).

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

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.

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

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

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

I would recommend reading articles about classpath as supplementary.

这篇关于Spring 找不到 bean xml 配置文件,当它确实存在时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆