在eclipse中配置项目,使其最终在tomcat“common”类加载器中

我有两个需要使用单例共享信息的tomcat Web应用程序.我已经通过将jared类放在tomcat公共目录中来使它工作.然后每个webapp获得单例的相同副本.我想做的是在
eclipse中集成这种行为.我希望公共类是一个单独的项目,每次我在eclipse中启动tomcat服务器时都会将其合并到tomcat公共类加载器中.任何人都知道如何配置eclipse来做到这一点? 最佳答案 可能是一种可能性为
extend the tomcat class loader,以便该类加载器在
other directories than WEB-INF/lib中搜索,这可以通过:

>扩展org.apache.catalina.loader.WebappClassLoader并覆盖findClassInternal方法.
>配置Tomcat以使用扩展的类加载器.
这是在Tomcat conf / Catalina / hostname路径下的相应webapp配置文件中完成的,其中包含以下元素:

  

然后在eclipse中,您可以在“构建路径上的必需项目”上设置您的公共项目,这使它成为类路径的一部分.

这意味着您的扩展类加载器必须能够查找其他classe:

>或者在固定的预定义路径中
>或在类路径中的预定义路径中.

没有经过自己测试,但可能会在这个问题上给你带来优势.

noselasd在评论中提出了一个更简单的解决方案,利用了Tomcat的GlobalNamingResources Component.

但是,FAQ does mentions

When you create a new Tomcat server in Eclipse, the New Server wizard assumes it is not safe to affect the current behavior of the Tomcat installation that this new server will use.

WTP is able to avoid affecting the behavior of the installed Tomcat by using Tomcat’s ability to run multiple server instances from a single installation. Thus, the default configuration for each new Tomcat sever you create will be a new server instance of the Tomcat installation associated with the Tomcat runtime selected in the wizard.
If you expect the new Tomcat server in Eclipse to run the same instance that the default batch files in your Tomcat installation run, you will likely be surprised when the Tomcat server in Eclipse doesn’t behave as expected.

The Tomcat server configuration can be changed so that it does run the same instance as your Tomcat installation.

你会在这里找到如何modify the server.xml in WTP.

点赞