注入静态 EJB,胡说八道? [英] Injecting a static EJB, nonsense?

查看:25
本文介绍了注入静态 EJB,胡说八道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写这段代码:

@Stateless
public class MyEjb
{
    @EJB
    private static MyOtherEjbWhichIWantStatic myOtherEjb;
}

出于各种原因,我想在我的类中注入一个 EJB 作为静态元素,这对我来说很有意义.

It makes sense to me, that I want to inject an EJB in my class, as a static element, for various reason.

不幸的是,Java 对此并不满意

Java is not very happy with that unfortunately

com.sun.enterprise.container.common.spi.util.InjectionException: Illegal use of static field private static MyOtherEjbWhichIWantStatic myOtherEjb on class that only supports instance-based injection

我不明白,为什么我不能将静态 EJB 注入另一个 EJB 中?

I don't get it, why can't I inject a static EJB into another EJB ?

推荐答案

正如其他人指出的,这是规范不允许的,简短的版本是仅支持 @EJB 批注对于具有 main() 函数的类中的静态成员(请参阅 EJB 3.0 规范和应用程序客户端容器).

As others pointed out, this is not allowed by the specification, and the short version is that the @EJB annotation is only supported for static members in classes with a main() function (see the EJB 3.0 specification and the app client container).

为什么会这样?首先,在 EJB 中完全禁止读/写静态字段(这是 EJB 限制的一部分).来自 为什么我不能在我的企业 bean 中使用非最终静态字段?

Why is it so? First of all, read/write static fields are totally forbidden in EJBs (this is part of the EJB restriction). From Why can't I use nonfinal static fields in my enterprise bean?

EJB 中不允许使用非最终静态类字段,因为此类字段使企业 bean 难以或不可能分发.静态类字段在特定类的所有实例之间共享,但仅在单个 Java 虚拟机 (JVM) 内共享.更新静态类字段意味着意图在类的所有实例之间共享该字段的值.但是,如果一个类同时在多个 JVM 中运行,则只有与更新实例在同一 JVM 中运行的那些实例才能访问新值.换句话说,与在多个 JVM 中运行相比,非 final 静态类字段在单个 JVM 中运行时的行为将有所不同.EJB 容器保留了跨多个 JVM(运行在同一服务器或任何服务器集群上)分发企业 bean 的选项.不允许使用非 final 静态类字段,因为企业 Bean 实例的行为会因它们是否分布而异.

Nonfinal static class fields are disallowed in EJBs because such fields make an enterprise bean difficult or impossible to distribute. Static class fields are shared among all instances of a particular class, but only within a single Java Virtual Machine (JVM). Updating a static class field implies an intent to share the field's value among all instances of the class. But if a class is running in several JVMs simultaneously, only those instances running in the same JVM as the updating instance will have access to the new value. In other words, a nonfinal static class field will behave differently if running in a single JVM, than it will running in multiple JVMs. The EJB container reserves the option of distributing enterprise beans across multiple JVMs (running on the same server, or on any of a cluster of servers). Nonfinal static class fields are disallowed because enterprise bean instances will behave differently depending on whether or not they are distributed.

如果这些字段被标记为 final,那么使用静态类字段是可以接受的做法.由于最终字段无法更新,因此容器可以分发企业 bean 的实例,而不必担心这些字段的值变得不同步.

It is acceptable practice to use static class fields if those fields are marked as final. Since final fields cannot be updated, instances of the enterprise bean can be distributed by the container without concern for those fields' values becoming unsynchronized.

但是虽然允许使用只读静态字段,但这不适用于 EJB.无状态 EJB 可能被池化,容器可能决定销毁它们(这是特定于实现的),并且您希望让容器选择您要使用的实例,尤其是在分布式环境中.换句话说,永远不要假设你被绑定到一个特定的实例.

But while using read-only static fields is allowed, this wouldn't be appropriate for EJBs. Stateless EJBs may be pooled, a container may decide to destroy them (this is implementation specific) and you want to let the container choose which instance you're going to use, especially in distributed environments. In other words, never assume you are tied to a particular instance.

所以最后,是的,这是无稽之谈.

So at the end, yes, this is a nonsense.

这篇关于注入静态 EJB,胡说八道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆