java - 静态成员是否在 Java EE 兼容的 servlet 容器中跨应用程序共享?

标签 java jakarta-ee servlets

如果我有一个 Servlet 类,并且这个类在两个应用程序中使用 - 的静态成员是否在两个应用程序之间共享? 此行为是由 Java EE 指定的还是特定于容器的?

最佳答案

If I have a Servlet class, and this class is used in two applications - are static members of shared across both applications?

不,静态成员不会跨应用程序共享。通常,每个应用程序都与其自己的类加载器相关联,因此,Servlet 类将在容器中加载两次。据推论,静态成员不会在应用程序之间共享。

如果您需要跨应用程序共享数据,建议根据您的需要使用文件、JMS 队列或数据库。

Is this behaviour specified by Java EE or container specific?

Java EE 6 平台规范没有定义类加载行为。规范在这方面规定如下:

EE.8.3 Class Loading Requirements

The Java EE specification purposely does not define the exact types and arrangements of class loaders that must be used by a Java EE product. Instead, the specification defines requirements in terms of what classes must or must not be visible to components.

组件可见的类和资源,不包括来自其他应用程序中其他网络模块的类。它们可能包括同一应用程序的其他网络模块中的类和资源:

EE.8.3.1 Web Container Class Loading Requirements

...

Components in the web container may have access to the following classes and resources. Portable applications must not depend on having or not having access to these classes or resources.

•The classes and resources accessible to any other web modules included in the same ear file, as described above.

...

由此推断,Servlet类如果部署在两个不同的应用程序中,将无法访问另一个应用程序中的另一个类。

关于java - 静态成员是否在 Java EE 兼容的 servlet 容器中跨应用程序共享?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6733326/

相关文章:

java - 将哪些方法实现到 "correct"DAO 类中?

java - 添加隐藏字段时 Spring 表单不提交

java - Google App Engine/del 网址错误

java - Tomcat HttpSession Cookie 共享

Java 客户端/服务器聊天室将消息作为对象发送

java - 如果我想访问 JasperSoftStudio 的属性,是否必须将 bean 导入到 JasperSoftStudio 的类路径中?

java - 如何使用 JEE7 Websockets 将参数传递给 @OnOpen 方法,

jakarta-ee - 如何在 JUnit 测试中使用 HK2 注入(inject) CDI 事件?

android - 解析数据时出错 org.json.JSONException : Unterminated array at character

java - 收集到方法参数?