Share Java classes between war files in Tomcat?

[亡魂溺海] 提交于 2019-12-01 20:39:08

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!