Spring classpath prefix difference

SIMPLE DEFINITION

The classpath*:conf/appContext.xml simply means that all appContext.xml files under conf folders in all your jars on the classpath will be picked up and joined into one big application context.

In contrast, classpath:conf/appContext.xml will load only one such file... the first one found on your classpath.


classpath*: It refers to a list of resources and loads all such files present in the classpath and list can be empty and if no such file is present in the classpath then application does not throw any exception(just ignores the error).

classpath: It refers to a certain resource and loads only the first file found on the classpath and if no such file is present in the classpath it will throw an exception

java.io.FileNotFoundException: class path resource [conf/appContext.xml] cannot be opened because it does not exist

The classpath*:... syntax is useful primarily when you want to build an application context from multiple bean definition files, using wildcard syntax.

For example, if you construct your context using classpath*:appContext.xml, the classpath will be scanned for every resource called appContext.xml in the classpath, and the bean definitions from all of them merged into a single context.

In contrast, classpath:conf/appContext.xml will obtain one and only one file called appContext.xml from the the classpath. If there is more than one, the others will be ignored.