1

I want to shared common application context in a multi-war Spring application, so i don't need to defined beans again and again. But i don't want to instantiate those beans defined in this common application context in each webapp. I only want to instantiate those beans once and share by all the webapps. Is it possible? Bare me if i'm wrong.

PS: this common application context exists in jar, which i'll import to every webapps.

1, Below article tell us how to share the common application context, but need to instantiate those beans in each webapp.

How to import spring-config.xml of one project into spring-config.xml of another project?

2, Below is another article i just read, i demo it, but still don't get what i want, beans got instantiated twice. Can someone check this section "Why would you want to use this?" in the article, i don't get it, do they have a solution there, can someone help me here, thanks a lot for your time.

http://spring.io/blog/2007/06/11/using-a-shared-parent-application-context-in-a-multi-war-spring-application/

here is the demo source code in second article: https://github.com/jasonluo/ceciic/tree/master/Research/multiple-contexts-sample

Updates

The problem of case2 is because i didn't deploy as a ear file, thanks Deinum for pointing this out, i'm using tomcat, so there is no way to achieve that.

Our solution now is using REST to access the service webapp which running in a separate server.

Community
  • 1
  • 1
gozizibj
  • 285
  • 1
  • 5
  • 23
  • If beans get instantiated multiple times you ahve something wrong in your setup. The blog has the right solution. – M. Deinum May 23 '14 at 07:55
  • Here is my output: `Hello from SampleWeb1 using service instance com.interface21.sample.multiplecontexts.service.SampleServiceImpl@753f67a9 Hello from SampleWeb1 using service instance com.interface21.sample.multiplecontexts.service.SampleServiceImpl@39654982`. It show me different instances. – gozizibj May 23 '14 at 07:59
  • 2
    Have you created an ear and deployed it? Or are you just deploying the 2 wars seperatly. It will only work if you package everything in an ear file. – M. Deinum May 23 '14 at 10:27
  • Hi Deinum, i do deploy 2 wars seperatly, thanks for pointing that out, will create an ear and try. i'll come back to you. – gozizibj May 23 '14 at 15:09

2 Answers2

1

Don't, there is usually classloader isolation going on to prevent this. Doing this right usually requires a thorough understanding of classloading, Java EE, packaging and your server.

Having that said there is way to do this as outlined in the blog you posted. You have to:

  • package both WARs into an EAR (this means you have to use and appserver like WildFly AS and can't just use a servlet engine like Tomcat or Jetty)
  • package both WARs as "skinny" WARs with at least Spring (and all its dependencies) and the shared beans (and all their dependencies) in the lib/ folder of the EAR instead of the WEB-INF/lib folder of the WARs.

Even then it depends on implementation details of the server. AFAIK it is not guaranteed to work under the Java EE specification. For example it likely won't work in GlassFish.

Update

I can't tell if the output of your demo is correct because I couldn't find a build file.

Yes, Tomcat does not support EARs, you could use Apache TomEE which is very similar and supports EARs (I don't know if TomEE does classloading in a way that makes this work). In theory you could also make use of the common classloader in Tomcat but that would be quite a hack.

Philippe Marschall
  • 4,452
  • 1
  • 34
  • 52
  • Hi Marschall, thanks for the reply. Does that means my demo have the correct output, the blog is just doing the same thing as the first article, right? I am using tomcat, looks like it's a impossible mission to me – gozizibj May 23 '14 at 08:18
  • Plain Tomcat doesn't support EAR files, only war files. You have to use a server that supports EAR files like TomEE, GlassFish or JBoss. It will only work in an EAR file or with a lot of trickery in the root/common classloader but that is a path you don't want to take. – M. Deinum May 23 '14 at 10:28
-1

There is a way to do it, using Spring Dynamic Modules, but it requires OSGi environment, which is quite different from simple Tomcat. Few articles worth reading:

That being said there is not a lot of up to date information about Spring with OSGi, but it's worth a try to achieve just what you said (but of course, with additional performance cost)

jderda
  • 890
  • 6
  • 18
  • Spring Dynamic Modules is obsolete and unmaintained. It has been donated to the eclipse foundatation and is now Eclipse Virgo. – M. Deinum May 23 '14 at 10:25