Why do I need to add "classpath:" to ApplicationContext?

40 views Asked by At

I'm trying to learn Spring Framework and reading book about it. I'm at ApplicationContext topic now and I don't understand why there is "classpath:" in ctx.load() method

GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); 
ctx.load("classpath:spring/app-context-xml.xml"); 
ctx.refresh();

It seems to work fine without adding this "classpath:".

2

There are 2 answers

0
Herry On BEST ANSWER

The main difference here, is when you are packaging your application. If the file specified is packed into the jar, it won't resolve it at runtime, as there is no dedicated file present. However, it will be present at the application's classpath.

1
wwadge On

That's coming from Resource. Other options would be using https:// for eg.

PS You are probably looking at an older version of Spring, configuring Spring via XML is quite out of fashion nowadays, in favour of annotations. Have a look at Spring Boot too.