Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enterprise bean environment-declare simple env entry using annotation

 
Amr Ouda
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dears,
Per specs , "Bean provider may use java language annotations or deployment descriptor to declare entries that are needed in the naming context"

I tried to use DD for creating a simple env entry, and the container created the env entry in the JNDI context ["java:comp/env/trialNumber"]

However when i removed the entry from the DD, used the bean code only to declare the env entry using @resource annotation ,and tried to get the env entry via JNDI lookup ...it didn't work.

Is @Resource used only to inject in target ref (without creating entries in JNDI context)
Or it can be used to create entries in JNDI context?


Working DD example :











This is the code that doesn't get any object (after removing entries from DD)







 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


The corresponding DD should have :
 
Amr Ouda
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's why i didn't get the object in the context tree.
Thanks Himai ,
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2535
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amr Ouda,

Great question!

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:

Regards,
Frits
 
Charles O'Leary
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

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


Background:

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.

...
Examples

@EJB public ShoppingCart myShoppingCart;

@Resource public DataSource myDB;

@Resource public UserTransaction utx;

@Resource SessionContext ctx;


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic