静态成员是否在符合 Java EE 的 Servlet 容器中的应用程序之间共享?
如果我有一个 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?
Is this behaviour specified by Java EE or container specific?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,静态成员不会在应用程序之间共享。通常,每个应用程序都与其自己的类加载器相关联,因此,Servlet 类将在容器中加载两次。由此推断,静态成员不会在应用程序之间共享。
如果您需要跨应用程序共享数据,建议根据您的需要使用文件、JMS 队列或数据库。
Java EE 6 平台规范没有定义类加载行为。规范对此做了如下规定:
对组件可见的类和资源不包括其他应用程序中其他 Web 模块的类。它们可能包含同一应用程序的其他Web模块中的类和资源:
由此推断,Servlet 类如果部署在两个不同的应用程序中,将无法访问另一个应用程序中的另一个类。
No, the static members will not be shared across applications. Typically, each application would be associated with its own classloader, and hence, the Servlet class would be loaded twice in the container. By inference, static members would not be shared across the applications.
If you need to share data across applications, it is recommended to use files, JMS queues or a database, depending on your needs.
The Java EE 6 Platform specification does not define class loading behavior. The specification states the following in this regard:
Classes and resources that are visible to components, do not include classes from other web modules in other applications. They might include classes and resources in other web modules of the same application:
By inference, the Servlet class, if deployed in two different applications, will not be able to access the other class in the other application.