Injecting a static EJB, nonsense?

后端 未结 3 1706
离开以前
离开以前 2020-12-06 11:12

I want to write this piece of code :

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

I

3条回答
  •  忘掉有多难
    2020-12-06 11:47

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

    Because the spec does not allow it:

    5.2.3Annotations and Injection

    As described in the following sections, a field or method of certain container-managed component classes may be annotated to request that an entry from the application component’s environment be injected into the class. [...] For all classes except application client main classes, the fields or methods must not be static.

    Note that the exception (application client main classes) only exists because those classes are never instantiated. Basically, static fields are generally problematic, and doubly so in an application server, because they can circumvent the separation of request handling threads and transactions, which are the whole point of using an application server in the first place.

提交回复
热议问题