简体   繁体   English

在多战Spring应用程序中使用共享的父应用程序上下文

[英]Using a shared parent application context in a multi-war Spring application

I want to shared common application context in a multi-war Spring application, so i don't need to defined beans again and again. 我想在一个多战的Spring应用程序中共享通用的应用程序上下文,所以我不需要一次又一次地定义bean。 But i don't want to instantiate those beans defined in this common application context in each webapp . 但是我不想在每个webapp中实例化在此通用应用程序上下文中定义的bean I only want to instantiate those beans once and share by all the webapps. 我只想实例化这些bean一次并由所有webapp共享。 Is it possible? 可能吗? Bare me if i'm wrong. 如果我错了就裸露我。

PS: this common application context exists in jar, which i'll import to every webapps. PS:这个常见的应用程序上下文存在于jar中,我将其导入到每个webapps中。

1, Below article tell us how to share the common application context, but need to instantiate those beans in each webapp. 1,下面的文章告诉我们如何共享通用的应用程序上下文,但是需要在每个webapp中实例化那些bean。

How to import spring-config.xml of one project into spring-config.xml of another project? 如何将一个项目的spring-config.xml导入另一个项目的spring-config.xml?

2, Below is another article i just read, i demo it, but still don't get what i want, beans got instantiated twice. 2,下面是我刚刚读过的另一篇文章,我对其进行了演示,但仍然没有得到我想要的东西,bean被实例化了两次。 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/ 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 这是第二篇文章中的演示源代码: 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. case2的问题是因为我没有部署为ear文件,感谢Deinum指出的这一点,我使用的是tomcat,所以没有办法实现。

Our solution now is using REST to access the service webapp which running in a separate server. 现在,我们的解决方案是使用REST访问在单独的服务器上运行的服务Webapp。

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. 正确地做到这一点通常需要对类装入,Java EE,打包和服务器有透彻的了解。

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) 将两个WAR都打包到EAR中(这意味着您必须使用WildFly AS之类的appserver和应用服务器,而不能仅使用Tomcat或Jetty之类的servlet引擎)
  • 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. 打包两次战争与至少春“瘦”战争(及其所有依赖关系),并在共享的豆(及其所有的依赖关系) lib/耳夹,而不是WEB-INF/lib战争的文件夹中。

Even then it depends on implementation details of the server. 即使如此,它仍取决于服务器的实现细节。 AFAIK it is not guaranteed to work under the Java EE specification. AFAIK不保证可以在Java EE规范下工作。 For example it likely won't work in GlassFish. 例如,它可能不适用于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). 是的,Tomcat不支持EAR,您可以使用非常相似并支持EAR的Apache TomEE (我不知道TomEE是否以使这项工作有效的方式进行类加载)。 In theory you could also make use of the common classloader in Tomcat but that would be quite a hack. 从理论上讲,您也可以使用Tomcat中的通用类加载器 ,但这确实是一个hack。

There is a way to do it, using Spring Dynamic Modules , but it requires OSGi environment, which is quite different from simple Tomcat. 使用Spring Dynamic Modules可以做到这一点,但是它需要OSGi环境,这与简单的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) 话虽这么说,有关OSGi的Spring并没有很多最新信息,但是值得您尝试实现您所说的内容(当然,这需要额外的性能成本)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM