注入静态EJB,废话? [英] Injecting a static EJB, nonsense?

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

问题描述

我想写这段代码:

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

对我来说,我想在我的课上注入一个EJB ,作为一个静态元素,出于种种原因。



Java不幸的是,不幸的是,

  com.sun.enterprise.container.common.spi.util.InjectionException:非法使用静态字段私有静态MyOtherEjbWhichIWantStatic myOtherEjb在类上只支持基于实例的注入

我没有得到它,为什么我不能将静态EJB注入另一个EJB?

解决方案

正如其他人指出的那样,规范是不允许的,而简短版本是 @EJB 注意只支持具有 main()函数的类的静态成员(请参阅EJB 3.0规范和应用程序客户端容器)。 p>

为什么会这样?首先,在EJB中完全禁止读/写静态字段(这是EJB限制的一部分)。从为什么我不能在企业bean中使用非正式静态字段? / a>


EJB中不允许使用非正式静态类字段,因为这样的字段使企业bean难以或不可能分发< STRONG>。静态类字段在特定类的所有实例之间共享,但仅在单个Java虚拟机(JVM)中。更新静态类字段意味着在类的所有实例中共享字段的值的意图。但是,如果一个类同时运行在多个JVM中,则只有与更新实例相同的JVM中运行的那些实例才能访问新值。换句话说,如果在单个JVM中运行,非虚拟静态类字段的行为会不同于在多个JVM中运行的字段。 EJB容器保留在多个JVM(在同一服务器上或任何一个服务器上运行)上分发企业Bean的选项。非直接静态类字段是不允许的,因为企业bean实例的行为会不同,取决于它们是否被分发。



如果这些字段是标记为 final 。由于最终字段无法更新,企业bean的实例可以由容器进行分发,而不用担心这些字段的值将不同步。


但是,在使用只读静态字段时,这是不适用于EJB的。无状态EJB可能被汇集,容器可能决定摧毁它们(这是实现具体的),并且您想让容器选择要使用的实例,特别是在分布式环境中。换句话说,永远不要假定你被绑在一个特定的实例上。



所以最后,是的,这是一个废话。


I want to write this piece of code :

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

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

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

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

解决方案

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).

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?

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.

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.

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天全站免登陆