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.




In my web.xml we have mentioned contextConfigLocation twice like below in this order. Could you pls tell which one will be picked up as contextConfigLocation file
contextConfigLocation
/WEB-INF/config/web-application-config.xml
dispatcher-servlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/config/dispatcher-servlet-config.xml
1
It’s a little difficult to see exactly what’s going on here, since your XML tags didn’t make it into the posted comment. But the way it looks to me right now, only the dispatcher-servlet-config.xml will be loaded since it is passed to the dispatcher servlet as a servlet init param (and all the beans will be loaded into the namespace of the servlet).
The web-application-config.xml will NOT be loaded, since you have not defined a ContextLoaderListener. If you DO include such a listener, then both context files will be loaded. The beans from web-application-config.xml will be available to the dispatcher-servlet and any other servlet you load; the beans from the dispatcher-servlet-config.xml will only be available to the dispatcher-servlet.
Hi Ben,
I have a issue with loading spring-aplication-context.xml.
it is clealry mentioned in web.xml also as:
classpath:spring/spring-aplication-context.xml
where spring is a folder inside which spring-aplication-context.xml resides.
The application is not getting loaded and says filenotfound for spring-aplication-context.xml.
The project is a maven project.
What can be the possible error?
Hi BenBourne,
Sounds to me like that “spring” directory is not on your classpath.
classpath refers to current directory in spring framework.
Err, no, it doesn’t. The classpath: prefix refers to the application’s classpath and means that resources will be retrieved from locations on that classpath (although at a given point in time that might indeed include the current working directory). An absolute location without a classpath: prefix is an absolute location and a relative location is relative to the working directory of the application.
ApplicationContext ctx =new ClassPathXmlApplicationContext(“../applicationContext.xml”);
BeanFactory beanFactory=ctx;
It works fine in my spring project
the above code is in /WEB-INF/jsp/index.jsp
but my applicationContext.xml is in /WEB-INF folder
Nice read! Thanks for taking the time out to compose this.
@Manish:
one of the common errors with maven deployment is to keep the context xmls in resources instead of webapp/WEB-INF – make sure you’re not doing the same.
Tapasvi
Sorry, I do not see this as a mistake. Maybe you can explain why that is a mistake?
Hey Jettro,
It is a “mistake” in the sense that, in and of itself, /WEB-INF is not on the classpath. So if you put your context files there and then try to load them by doing something like “classpath:*context.xml” rather than “classpath:/WEB-INF/*context.xml”, then it won’t work. And this is an easy mistake to make, since the DispatcherServlet implicitly initiates the processing of a /WEB-INF/-servlet.xml file, sort of suggesting that /WEB-INF is where you should put the context files.
Also, one might consider it a “mistake” against the style that SpringSource introduced with Spring Roo of always placing context files in the META-INF/spring directory (which, to be honest, is where you *should* put context files that are in a JAR).
Finally, it is definitely a mistake if you put the files in a /webapps/WEB-INF directory when your maven project does not have a “war” packaging. But, uhhhh……
Hmm, I see I mist the whole thread of comments. Did not notice it was a response to another comment. My bad
related to SOM’s Question, I am trying to use 2 XML files in application context – one is in the same project and 2nd in another project. This 2nd project has been added to my current project and the context in web.xml is
contextConfigLocation
/WEB-INF/eu24ui-servlet.xml
classpath:spring-ds.xml
classpath:spring-hibernate-iocm.xml
But when I try to start the jboss server.. it throws fileNotFoundException.
Need help.
Manish,
In my experience the JVM doesn’t lie to you when reporting errors. If it’s throwing a FileNotFoundException, the file simply isn’t being found.
I’d start by checking out options to print the classpath on server and application start (there’s sure to be a diagnostic option for this), then take a good look at your package. From your description I’m willing to bet your second project package isn’t on the classpath of the first.
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