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.
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.
resources get copied to the class package when you do the above
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
UPDATE
put the package with the bean.xml
into the src/main/resources
. It should work with the path your using.
UPDATE 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? *
The thing is, the Spring container will look from the class root. It has nothing to with the resources
folder. The resources
is a convenience dir for maven projects to build to your class path. The reason the tutorial works, is that the beans.xml
is in a package, that will get put into the class path in the build, as seen below. It is only preferred to use a resources
, but a package` will also build to the class path.
Best Answer
You should keep your Spring files in another folder, marked as "source" (just like "src" or "resources").
WEB-INF is not a source folder, therefore it will not be included in the classpath (i.e. JUnit will not look for anything there).