0

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?

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, but I should maybe avoid pinpointing the filelocation since I want to be able to move the server later on.
Sure, it depends on your use case. But to just "get it to work" I figured this might help you out.
0
new ClassPathXmlApplicationContext(this.getClass().getResource("/spring.xml").getPath())

try the code above

hope that helped

1 Comment

Thank you! Changed it to new ClassPathXmlApplicationContext(ClassName.class.getResource("/spring.xml").getPath()); since it's in a static method.
0

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.

1 Comment

Ok, I may have misunderstood where Spring looks from answeres to similiar questions.
0

Use "FileSystemXmlApplicationContext" as

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.