Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - share web application context between different webapps

Tags:

java

spring

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?

like image 997
Miguel Ping Avatar asked Nov 20 '09 11:11

Miguel Ping


1 Answers

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

like image 51
Aaron Digulla Avatar answered Sep 30 '22 14:09

Aaron Digulla