Sharing an application context between two WARs?

2020-02-25 09:08发布

Is there a way to share an app-context between two deployed wars? One war needs to wire-in the services of another, and I don't know where to start with this.

7条回答
Rolldiameter
2楼-- · 2020-02-25 09:14

There is an informative blog post I think you should check out: Using a shared parent application context in a multi-war Spring application

查看更多
甜甜的少女心
3楼-- · 2020-02-25 09:18

Agreed with @RichardSmith, the accepted answer was not helpful for me. While Richard's answer was helpful in pointing me in the right direction, I'm using Jetty so I can't use EAR. I ended up using the server context to share an object(s) between wars - works even with hot-(re)deployed webapps:

https://stackoverflow.com/a/46968645/1287091

Probably a way to adapt this for Tomcat as well.

查看更多
太酷不给撩
4楼-- · 2020-02-25 09:21

The general purpose of a web application container like tomcat is that each application is able to run independently (so you can stop and start individual apps without affecting others), which means that there has probably been a lot of effort put into their development specifically to prevent you from doing this. So even if you find a loophole, I would suggest against using it unless it is recommended or at least sanctioned by the designers.

I suggest you start looking for other solutions. For example:

  • Do you really need to share the object instances? If they are stateless you may not need to, and you can simply run a copy of the context in each app.
  • Can you separate the code that you're trying to share into a third WAR that exposes a rest service? Or maybe one of the existing WARs could act as the service.
查看更多
你好瞎i
5楼-- · 2020-02-25 09:21

You could possibly bind a context to JNDI if it doesn't exist.

The price for this is that each webapp's context somehow has to be aware that it might not be the primary.

Also, you need to be really careful about race conditions, e.g.

  • first webapp starts, detects that there is no context
  • first webapp starts to build the context
  • second webapp starts, detects that there is no context
  • second webapp starts to build the context
  • first webapp finishes to build the context, binds it
  • second webapp finishes to build the context, binds it
  • congratulations, you have lost all beans from the context of the first webapp
查看更多
虎瘦雄心在
6楼-- · 2020-02-25 09:26

There is maybe a way if you start an embedded jetty server and access both web apps from the class where you start and configure the jetty server.

See:

Embedding Jetty

查看更多
够拽才男人
7楼-- · 2020-02-25 09:27

Do you need to share the runtime context, or do you want to just re-use bean definitions across the two applications?

If it is only the latter then you could easily extract the common Spring context XML files into some shared dependency, and just re-use the JAR across the two webapps. However if you need the beans/services in each application to talk to each other, you need a different option.

查看更多
登录 后发表回答