0

I have a default package called com.voja.springtest and another one called com.voja.springtest.beans where I have an beans.xml file.

I can get it like so using FileSystemXmlApplicationContext :

ApplicationContext context = new FileSystemXmlApplicationContext("C:/Users/Voja/Desktop/_/vj/springtest/src/main/java/com/voja/springtest/beans/beans.xml");

But ClassPathXmlApplicationContext can't find it like so (and it should per the tutorial I am doing):

ApplicationContext context = new ClassPathXmlApplicationContext("com/voja/springtest/beans/beans.xml");

Why?

0

2 Answers 2

1

you use wrong parh , in your case it should be like :

ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:beans.xml");

4.7.2.2 The classpath*: prefix

When constructing an XML-based application context, a location string may use the special classpath*: prefix:

ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:conf/appContext.xml"); This special prefix specifies that all classpath resources that match the given name must be obtained (internally, this essentially happens via a ClassLoader.getResources(...) call), and then merged to form the final application context definition.

The Classpath*: portability classpath*: prefix

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

2 Comments

Thanks, this seems to find it but now it complains that it cannot find the actual bean, and with FileSystem I had no problem whatsoever. Any idea why classpath can find the xml file but not the bean defined within it? Once again, it worked with FileSystem.
move this file beans.xml into 'C:/Users/Voja/Desktop/_/vj/springtest/src/main/resources'. resources - it's right place for config
0

FileSystemXmlApplicationContext picks the XML file from absolute path by appending keyword "file" and also can fetch from classpath by appending keyword "classpath". You can access the file as below

ApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring-app.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.