Spring - 在不同的webapps之间共享Web应用程序上下文 [英] Spring - share web application context between different webapps

查看:85
本文介绍了Spring - 在不同的webapps之间共享Web应用程序上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多模块maven项目。其中一个模块是一个包含一些spring bean的util层。我想在其他模块中共享相同的 spring bean。

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.

其他模块部署为不相关 web应用程序,所以理想情况下我的util bean将是单例,我只会在所有网络应用程序中有一个对这些单例的引用。

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.

我找到了一些链接共享spring web应用程序上下文,但似乎它们在同一个.ear中工作,但在我的情况下,我有不同的web应用程序。

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?

推荐答案

不容易。应用程序服务器每个应用程序使用一个类加载器,这意味着即使您设法将创建bean的引用传递给另一个bean,也会得到 ClassCastException s 。

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.

您需要的是在应用服务器启动时定义bean。将bean的代码放入app服务器的类路径中,并检查文档如何设置JNDI上下文。 JNDI上下文允许您共享全局资源(如数据库连接)。

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

这篇关于Spring - 在不同的webapps之间共享Web应用程序上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆