javaspringapplicationcontext

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


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:".


Solution

  • 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.