5

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 Answers 3

4

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).

Sign up to request clarification or add additional context in comments.

2 Comments

"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.
I'll have to give the JNDI technique a try.
2

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.

1 Comment

Yes, I am talking about that technique, although I didn't manage to get it working...
-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.