The Spring classpath:-notation

As most of you probably know, the Spring Framework can load all sorts of resources into its application context from all sorts of sources. Especially it can do this from the classpath and the people over at SpringSource have even introduced their own URI “protocol” to make this easy: you can identify resources using the classpath-URIs, like so:

classpath:<relative path to resource here>

Most of you are probably familiar with this notation, because it is often used when loading the application context files themselves using the ContextLoaderListener.

What many people don’t realize, is that the classpath:-notation is far more versatile than just for use as a standard URI, because you can use wildcards. In fact I only recently learned this myself from a colleague (at least in part) — which is kind of dumb, because it’s mentioned quite clearly in the documentation.

The first thing you can do above and beyond loading resources by their full path (relative to the classpath) is use all the Ant-style wildcards. So instead of loading as follows:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:businessContext.xml
classpath:persistenceContext.xml
classpath:webFlowBeansContext.xml
classpath:etc.
</param-value>
</context-param>

you can simply load as follows:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:*Context.xml
</param-value>
</context-param>

or, as the case may be:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:**/*Context.xml
</param-value>
</context-param>

There is a catch to this strategy, however: if you have multiple resource repositories (directories, databases, online resources, whatever) on your classpath containing files that match your wildcard pattern, Spring will only look for matching resources in one of these locations. To illustrate this for yourself, try using maven to create a standard project structure, then put a context file in src/main/resources and src/test/resources and try to load both at the same time with a wildcard — you’ll only get one.

The classpath*:-notation

Of course the Spring guys wouldn’t be the Spring guys if they didn’t have a simple solution for this problem. It’s called the classpath*:-notation. Simply put, if you rewrite the snippet from above like so:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:**/*Context.xml
</param-value>
</context-param>

then Spring will load every matching context file from the entire classpath.

As the documentation points out, the combination of the classpath*:-notation with Ant-style wildcards makes it possible to organize your application into a neat component-assembly style: you can put all the code for each individual problem from your problem domain into its own component (a jar, say) and then “slot” as many jars into place as you like to make your application. You can even add components later on without making major adjustments to any other component (if you think about it carefully). Just make sure each component has an application context file that follows the general naming schema. Think of the possibilities if you include OSGi or even the new Service Provider spec.

Caveat emptor

There is a very specific sort of naming pattern that you can also create using the classpath*:-notation. This is the naming pattern whereby all application context files within the application have exactly the same name (e.g. classpath*:myAppContext.xml). This pattern allows you to expand your context completely transparently to all others without even having to think very hard about the naming convention.

DO NOT EVER, EVER, EVER, EVER, EVER, EVER, EVER, EVER, EVER, EVER, EVER, EVER DO THIS!!!

If you give all your context files the same name, you lose precise control over what gets loaded and what doesn’t. This can hurt if you create components that you want to reuse across applications (especially if you are using Maven and have transitive dependenciesto deal with) and it can hurt in creating integration tests that involve partially loaded contexts. It also makes it harder to find naming conflicts in your contexts if you introduce them accidentally (especially if you are not the only developer on your project). Always make sure each context file on the classpath has a unique name!

Conclusion

The Spring Framework includes a very flexible notation for loading resources from the classpath, using wildcards to identify resources. This notation, if used correctly, gives you considerable freedom in creating component-assembly style applications. However, you must still guard against using unwise naming of your context files.

Spring application context loading tricks
Tagged on: