在Tomcat中的war文件之间共享Java类? [英] Share Java classes between war files in Tomcat?

查看:183
本文介绍了在Tomcat中的war文件之间共享Java类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Tomcat 7作为servlet容器,我有两个war文件(Eclipse中的两个项目)。问题是,我想在两个已部署的war文件之间共享类。
但据我所知,Tomcat / Servlet规范是不可能的。两个部署的Web应用程序似乎彼此隔离。

I am using Tomcat 7 as a servlet container and I have two war files (two projects in Eclipse). The problem is, that I want to share classes between both deployed war files. But this is not possible with Tomcat / Servlet specifications as far as I know. Both deployed web applications seems to be are isolated from each other.

所以我从JBoss读取,我是新手,作为应用程序服务器,我可以在其中部署ear文件(所以我可以将多个war文件放入其中)然后我可以在它们之间共享类?

So I read, I am a newbie, from JBoss as an application server in which I can deploy ear files (so I can put multiple war files into it) and then I can share classes between them?

所以我的问题:你是否同意我的陈述,是否有可能用JBoss做到这一点?我是否必须进行更改,或者我可以轻松地将其放入ear文件并使用JBoss进行部署并且它可以工作吗?

So my question: Do you agree with my statement, is it possible to do this with JBoss? And do I have to make changes or can I put easily both in an ear file and deploy it with JBoss and it will work?

最好的问候,Tim。

推荐答案

有2个简单的选项。两者都要求您在单独的项目(JAR)中定义共享类,我们称之为 SHARED 。现在,在部署方面,您有两个选择:

There are 2 easy options. Both require you to define the shared classes in a separate project (JAR), let's call it SHARED. Now, when it comes to deployment you have 2 options:


  1. SHARED.jar 复制到Tomcat的lib文件夹中你已经完成了。

  2. 在你的WebApp中引用 SHARED.jar ,当从eclipse导出你的WAR时,工具会创建一个JAR,把它放在WebApp的WEB中 - INF / lib文件夹,并创建WAR(见截图)。

  1. copy SHARED.jar to Tomcat's lib folder and you're done.
  2. reference SHARED.jar in your WebApp, when exporting your WAR from eclipse the tooling will create a JAR, place it in WebApp's WEB-INF/lib folder, and create the WAR (see screenshot).

如果选择#1选项,重要的是要了解WebApp类加载器(Tomcat系统类加载器)之间共享相同的类,因此只有在想要在不同WebApp(例如单例)之间共享状态时才使用此选项。

If you go with option #1 it is important to understand that the same classes are shared between the WebApp classloaders (Tomcat system classloader), so you'd go with this option only if you want to share state between different WebApps (e.g. Singletons).

顺便说一句:截图是使用Eclipse 3.6 + WTP ...

BTW: The screenshot is made using Eclipse 3.6 + WTP...

这篇关于在Tomcat中的war文件之间共享Java类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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