I guess java:comp/env is for the environment entries (String, Integer, Double....) defined in the DD or a bean via lookup method and SessionContext annotated with @Resource.
On Frits's notes, there is an example to inject a bean echo:
I guess injecting a field (eg. integer, String) won't work with @Resource and lookup without the environment entry defined in DD.
If you want to inject a field (eg integer) , you have to do the following just like Frits' example on p.70:
You are right it is a bit confusing in the EJB specs:
The environment entries are declared by the Bean Provider by means of annotations on the bean class or in the deployment descriptor.
This only declares the environment entry, but it doesn't say anything about the registration in the ENC.
There is a small sentence later on in the specs:
The env-entry-value element in the deployment descriptor is optional when an injection target is specified. If the element is not specified, no value will be injected. In addition, if the element is not specified, the named resource is not initialized in the naming context, and explicit lookups of the named resource will fail.
So the conclusion for simple environment entries is: The resource is only registered in the naming context if the env-entry is declared with a env-entry-value in the deployment descriptor.
You can easily list the Naming Context (ENC) entries inside your EJB method with this code snippet:
Given that the original code (or any similar) was not described as failing and with container implementations and (any partial or full) deployment descriptor aside , can (or perhaps more appropriately how) @Resource be used to declare (and/or create) entries in the JNDI context without need for any deployment descriptor entries (whether simple environmental entries or not)? (Are environmental entries somehow exceptional? And/or where does the spec "single out" simple annotation-based only environmental entries?)
Annotations and Deployment Descriptors ....
Environment entries may be declared by the use of annotations, without need for any deployment
descriptor entries. Environment entries may also be declared by deployment descriptor entries, without
need for any annotations.
Annotation of Instance Variables
The developer may annotate instance variables of the enterprise bean class to indicate dependencies
upon resources or other objects in the bean’s environment. The container automatically initializes these
annotated variables with the external references to the specified environment objects. This initialization
occurs before any business methods are invoked on the bean instance and after the time the the bean’s
EJBContext is set.
@EJB public ShoppingCart myShoppingCart;
@Resource public DataSource myDB;
@Resource public UserTransaction utx;
@Resource SessionContext ctx;
Anderson gave himself the promotion. So I gave myself this tiny ad: