The road so far….

April 3, 2010

Spring:Classpath and the world of Classloaders

Filed under: java — Tags: , — Rahul Sharma @ 1:22 pm

A while ago I re-factored an existing application in a multi module maven project. These modules were being loaded into a Gigaspace container using spring. Gigaspace used multiple classloaders to load the applications at different levels. The re-factored the application was being assembled as dependent jars from one module to other, so I had different xml context files that were being imported using :

<import resource=”classpath:moduleOne.xml”/>

kind of notation. Things were fine till this point as all the juints ran fine.

The frustration started when I tried to run  this and found out that the container was unable to load the resources. I tried a few hacks but  hacks are not the solution and nothing eventually solved out of that.

A little bit of more googling landed me on Spring documentation and then I felt quite dumb.  The solution was right there “use the wildcard notation” :

<import resource=”classpath*:moduleOne.xml”/>

This worked!!

As per the documentation the wildcard notation relies on the implementation of the underlying class loader. Gigaspace uses different classloaders at different levels,  as it loads different components of the application. The  classpath* notation tells the current loader to load it from it s class path or any of the loaders being used. This also poses a kind of problem, if  you have multiple context files having the same name  you lose control over what is being loaded.

So the bottomline is have different names for your context files and load them using the classpath*: notation as import statements.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: