|
||||||
Spring application context loading tricksThe Spring classpath:-notationAs 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:
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:
you can simply load as follows:
or, as the case may be:
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:
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! ConclusionThe 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. 6 comments to Spring application context loading tricks |
||||||
|
Copyright © 2010 Gridshore - All Rights Reserved |
||||||
Hi Ben,
Very nice article. I was looking for the answer described in “Do not ever do this” where I have multiple modules with same application context name. I will use module specific names.
What if I want to load two application context files with dependencies like applicationContextTwo uses beans in applicationContextOne and both are in different classpaths.
Som,
If you want to load files from two different locations using wildcards, the classpath*:-notation is the way to go.
As for your cross-contextfile dependencies, those will always work; that’s a function of the Spring container. The thing to understand is that you do not get a context PER FILE — no matter how many context FILES you have, the Spring container always loads them all together and makes one single CONTEXT out of them before initializing your beans. The only way to have multiple contexts in a program is to have the program start two separate instances of the Spring container.
Hehe Ben like always a great read
Thanks!
I agree with Uri. Note that my post was intended as an overview and not so much as a recommendation.
An advantage of explicitly defining the actual application context names (without wildcards) is that it provides you with yet another view of your layered architecture. I personally like to have an application context per layer (e.g. service-layer.xml, data-access-layer.xml, etc…) and each one of these application context is composed out of more functional ones. so all thing related to messaging I would put in messaging.xml and import it in the service-layer.xml context.
cheers,
Uri