views:

248

answers:

3

The JEE specification states that an EJB injection like this:

@EJB MyInterface myBean;

will create en entry in the Enterprise Naming Context, viz. java:comp/env/.MyInterface/myBean. It is up to the deployer to bind this context entry to a real value and to inject this value in the myBean field.

Now I have the feeling I am missing something here:

Why is the context entry necessary? An instance of the requested EJB will be injected, so why is the entry in the context needed? Why does the injection has to happen via the context entry?

A: 

I think if you need to inject an Stateful Session Bean you gonna need some context that knows the relation between your EJB instance and some previously injected EJB dependency (to that instance).

diega
A: 

It is to allow you lookups by somehow obtained string.

Mykola Golubyev
A: 

All the received answers did not address the issue: why is an ENC entry needed if the injection annotation gives enough information to resolve the injection (so my first thought was that it was redundant).

The answer is that the injection can be overridden in the deployment descriptor. This is because the EJB standard defines developer roles.

It assumes that the "bean provider" can request an injection, even provide default values. But the "application assembler" can override these values. The "bean provider" is assumed to use annotations or XML, and the "assembler" is assumed to mainly use the XML.

That is why the EJB standard defines this relation between the ENC and an injection.

Bruno Ranschaert