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

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

问题描述

我想写这片code的:

I want to write this piece of code :

@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上运行,比它会在多个JVM上运行。 EJB容器保留跨多个JVM分布企业Bean的选项(在同一台服务器上运行,或者在任何一个服务器集群)。非最终静态类字段是不允许的,因为企业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.

这是使用静态类领域,如果这些字段被标记为最后可以接受的做法。因为最后字段不能被更新,企业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天全站免登陆