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

查看:3548
本文介绍了当它存在时,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:

    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

但我没有得到它,因为我做以下代码测试:

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

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

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

我建议阅读关于classpath的文章作为补充。

I would recommend reading articles about classpath as supplementary.

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

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