简体   繁体   English

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

[英]Are static members shared across applications in a Java EE compliant servlet container?

If I have a Servlet class, and this class is used in two applications - are static members of shared across both applications?如果我有一个 Servlet class,并且这个 class 用于两个应用程序 - static 的成员是否在两个应用程序之间共享? Is this behaviour specified by Java EE or container specific?此行为是由 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 class,并且这个 class 用于两个应用程序 - static 的成员是否在两个应用程序之间共享?

No, the static members will not be shared across applications.不,static 成员不会在应用程序之间共享。 Typically, each application would be associated with its own classloader, and hence, the Servlet class would be loaded twice in the container.通常,每个应用程序都将与它自己的类加载器相关联,因此,Servlet class 将在容器中加载两次。 By inference, static members would not be shared across the applications.通过推断,static 成员不会在应用程序之间共享。

If you need to share data across applications, it is recommended to use files, JMS queues or a database, depending on your needs.如果您需要跨应用程序共享数据,建议使用文件、JMS 队列或数据库,具体取决于您的需要。

Is this behaviour specified by Java EE or container specific?此行为是由 Java EE 还是特定于容器指定的?

The Java EE 6 Platform specification does not define class loading behavior. Java EE 6 平台规范未定义 class 加载行为。 The specification states the following in this regard:该规范在这方面规定了以下内容:

EE.8.3 Class Loading Requirements EE.8.3 Class 加载要求

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. Java EE 规范故意没有定义 Java EE 产品必须使用的 class 装载机的确切类型和布置。 Instead, the specification defines requirements in terms of what classes must or must not be visible to components.相反,规范根据哪些类必须或不能对组件可见来定义要求。

Classes and resources that are visible to components, do not include classes from other web modules in other applications.对组件可见的类和资源,不包括来自其他应用程序中其他 web 模块的类。 They might include classes and resources in other web modules of the same application:它们可能包括同一应用程序的其他 web 模块中的类和资源:

EE.8.3.1 Web Container Class Loading Requirements EE.8.3.1 Web 集装箱 Class 装载要求

... ...

Components in the web container may have access to the following classes and resources. web 容器中的组件可以访问以下类和资源。 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. • 如上所述,同一ear 文件中包含的任何其他web 模块可访问的类和资源。

... ...

By inference, the Servlet class, if deployed in two different applications, will not be able to access the other class in the other application.通过推断,Servlet class 如果部署在两个不同的应用程序中,将无法访问另一个应用程序中的另一个 class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM