Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring classpath prefix difference

People also ask

What is classpath prefix?

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.

What is the classpath in Spring boot project?

It's a path inside your project where you place resources. During the build step, Maven will take files in there and place them in the appropriate place for you to use them in your runtime classpath, eg in an executable . jar , some physical file system location used in the classpath (with java 's -cp option), etc.

How do you describe the path to the resource loaded from the classpath?

We can specify different prefixes for creating a path to load resources from different locations. To load a resource from a file system, we use the file prefix. Similarly, to load a resource from the classpath, we use the classpath prefix. We may also specify a URL as a resource path.


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.


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.


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 source code of Spring:

public Resource[] getResources(String locationPattern) throws IOException {
   Assert.notNull(locationPattern, "Location pattern must not be null");
   //CLASSPATH_ALL_URL_PREFIX="classpath*:"
   if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {
      // a class path resource (multiple resources for same name possible)
      if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) {
         // a class path resource pattern
         return findPathMatchingResources(locationPattern);
      }
      else {
         // all class path resources with the given name
         return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));
      }
   }
   else {
      // Only look for a pattern after a prefix here
      // (to not get fooled by a pattern symbol in a strange prefix).
      int prefixEnd = locationPattern.indexOf(":") + 1;
      if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {
         // a file pattern
         return findPathMatchingResources(locationPattern);
      }
      else {
         // a single resource with the given name
         return new Resource[] {getResourceLoader().getResource(locationPattern)};
      }
   }
}