tags:

views:

554

answers:

3

Hi, I have a multi-module maven project. One of the modules is a util layer that has some spring beans. I want to share the same spring beans within the other modules.

The other modules are deployed as non-related web-applications, so ideally my util beans would be singletons and I would only have one ref to these singletons throughout all the web apps.

I have found some links for sharing spring web application contexts, but it seems that they work within the same .ear, but in my case I have different web apps.

Is there a way of accomplishing this?

+3  A: 

Not easily. Application servers use one class loader per application which means that you'd get ClassCastExceptions even if you'd manage to pass a reference from the one which created the beans to another one.

What you need is to define the beans at the time when the app server starts. Put the code for the beans into the classpath of the app server and check your documentation how to setup a JNDI context. The JNDI context allows you to share global resources (like DB connections).

Aaron Digulla
"Application servers use one class loader per application"... not necessarily. JavaEE mandates this, but some appservers (e.g. JBoss) allow you to have one classloader per EAR, which vastly simplifies things.
skaffman
I'll have to give the JNDI technique a try.
Miguel Ping
A: 

Depending on the application server / servlet container, you can google for techniques of sharing the ServletContext between webapps. The WebApplicationContext is stored in the ServletContext, so you will have your answer.

Bozho
+1  A: 

I assume you're referring to techniques like this. As far as I know that should be possible outside an EAR. But there's no official standard for it. In practice, you'll have to make sure that the Spring JAR files are loaded in the shared context loader for your servlet container and not in the individual applications' contexts. I believe the XML file will also need to be in that parent context's classpath.

Dan
Yes, I am talking about that technique, although I didn't manage to get it working...
Miguel Ping