I want to write this piece of code :
@Stateless
public class MyEjb
{
@EJB
private static MyOtherEjbWhichIWantStatic myOtherEjb;
}
I
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.