Spring cannot find bean xml configuration file whe

2019-01-07 05:30发布

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.

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?

13条回答
趁早两清
2楼-- · 2019-01-07 05:58

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:

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

it still was an IO Exception.

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.

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.

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

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

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.

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.

查看更多
闹够了就滚
3楼-- · 2019-01-07 05:59

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.

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

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

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

查看更多
三岁会撩人
4楼-- · 2019-01-07 06:02

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.

查看更多
地球回转人心会变
5楼-- · 2019-01-07 06:07

src/main/resources is 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

new ClassPathXmlApplicationContext("beans.xml")

Or like this

new GenericXmlApplicationContext("classpath:beans.xml");
查看更多
我命由我不由天
6楼-- · 2019-01-07 06:11

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.

查看更多
爷的心禁止访问
7楼-- · 2019-01-07 06:13

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

How I was using

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.

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

查看更多
登录 后发表回答