Abrakanoodle
Abrakanoodle

Reputation: 3

ClassPathXmlApplicationContext error, Spring framework

I have encountered a problem when it comes to the Springs framework, which leads to that the communication between the server and the database does not work.

The project that I created is a Spring project, then refactored to Maven.

At this line in the code: ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("projectName/spring.xml");

I get this error: Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [projectName/spring.xml]; nested exception is java.io.FileNotFoundException: class path resource [projectName/spring.xml] cannot be opened because it does not exist

But it does exist. And I've tried solutions for this problem such as writing ClassPathXmlApplicationContext("spring.xml") instead. This doesn't help however, since then Spring automatically looks in the folder src/main/resources. This doesn't work for me since my project structure doesn't allow me to add this folder and put a XML-file in it. If I try to create this folder, then it is automatically put inside the Java-resources folder, and Eclipse won't allow me to put XML in there.

This is how my project looks: enter image description here

Is there a way for me to declare where Spring should look for this spring.xml-file?

Upvotes: 0

Views: 3711

Answers (4)

Alferd Nobel
Alferd Nobel

Reputation: 3979

Use "FileSystemXmlApplicationContext" as

ApplicationContext  context =  new FileSystemXmlApplicationContext("spring.xml");

Upvotes: 0

v.ladynev
v.ladynev

Reputation: 19956

Spring doesn't look at the src/main/resources, it looks at the classpath.

If you write projectName/spring.xml you need to have this file in bin/projectName/spring.xml or build/projectName/spring.xml. Where bin or build your build folder.

If you build a jar, this file should be in the jar!projectName/spring.xml.

For the web-application this file should be in the WEB-INF/classes/projectName/spring.xml.

If you add src/main/resources at the class path, then content of this folder will be in the build folder. Maven adds src/main/resources at the class path automatically.

Sometimes you should rebuild (clean) your project in the IDE to have such files in the build folder.

Upvotes: 0

Jonck van der Kogel
Jonck van der Kogel

Reputation: 3293

The ClassPathXmlApplicationContext assumes that the file is on your classpath (Javy describes how to do load a resource from your classpath).

If you want to load the configuration from your file system (as you're doing), you might want to consider using FileSystemXmlApplicationContext instead. Using this mechanism to load your context you can pass a file system location as you're currently doing.

Upvotes: 1

Javy
Javy

Reputation: 964

new ClassPathXmlApplicationContext(this.getClass().getResource("/spring.xml").getPath())

try the code above

hope that helped

Upvotes: 0

Related Questions