• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Questions on Enterprise Bean’s Environment as a JNDI Naming Context

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

16.2.2 Annotations for Environment Entries



A field or method of a bean class may be annotated to request that an entry from the bean s environment
be injected. Any of the types of resources or other environment entries described in this chapter may
be injected. Injection may also be requested using entries in the deployment descriptor corresponding to
each of these resource types.



A.A field of the bean class may be the target of injection. The field must not be final. By default, the name of the field is combined with the name of the class in which the annotation is used and is used directly as the name in the bean’s naming context.



Please advise if my understanding of the Below statement is correct or not.

Statement:A field of the bean class may be the target of injection

Understanding:Does it mean "using Bean Field as a resource ,creating a environmental entry for it and then using it in other fields and methods of other classes".

B.Environment entries may also be injected into the bean through bean methods that follow the naming conventions for JavaBeans properties. The annotation is applied to the setmethod for the property, which is the method that is called to inject the environment entry. The JavaBeans property name (not the method name) is used as the default JNDI name. For example, a method named setMyDatabasein the same MySessionBeanclass would correspond to the JNDI name java:comp/env/com.example.MySessionBean/myDatabase



Statement:Here,Author i saying about "Environment entries may also be injected into the bean through bean methods that follow the naming conventions for JavaBeans properties. The annotation is applied to the setmethod for the property, which is the method that is called to inject the environment entry"

Understanding:However,the next line "java:comp/env/com.example.MySessionBean/myDatabase" looks like using the method name as resource creating a environmental entry for it and then using it in other fields or methods of other classes.
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mohit,

Statement:A field of the bean class may be the target of injection
Understanding: Does it mean "using Bean Field as a resource ,creating a environmental entry for it and then using it in other fields and methods of other classes".


Yes, after the container has injected a reference to a resource in your bean field you can then use this reference in other fields and methods. The environmental entry that will be created by the injection is a sort of local address to the resource and can also be used by a lookup method.

Understanding:However,the next line "java:comp/env/com.example.MySessionBean/myDatabase" looks like using the method name as resource creating a environmental entry for it and then using it in other fields or methods of other classes.


No the method name is setMyDatabase(), the property is called myDatabase.

Regards,
Frits
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the Reply but I still have some queries as follows:

Question 1:Why is the JNDI name(java:comp/env/com.acme.example.MySession-Bean/myDatabase) created for the field of Bean class/bean methods to which a resource is injected instead of creating JNDI name for the actual resource ?

Question 2:If field or the method request injection of a resource of a different (non-default) name,Then How can one differentiate b/w the JNDI name for method and field (In below 2 statements,it seems JNDI name doesn't changes with change in a different resource injection)?

In the example given in section 16.2.2,

a)method named setMyDatabasein the same MySessionBeanclass would correspond to the JNDI name java:comp/env/com.example.MySessionBean/myDatabase
b)a field named myDatabasein the class MySessionBeanin the package com.acme.example would correspond to the JNDI name java:comp/env/com.acme.example.MySession-Bean/myDatabase

and

Each resource may only be injected into a single field or method of the bean. Requesting injection of the java:comp/env/com.example.MySessionBean/myDatabaseresource into both the setMyDatabasemethod and the myDatabaseinstance variable is an error. Note, however, that either the field or the method could request injection of a resource of a different (non-default) name.


 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mohit,


Question 1:Why is the JNDI name(java:comp/env/com.acme.example.MySession-Bean/myDatabase) created for the field of Bean class/bean methods to which a resource is injected instead of creating JNDI name for the actual resource ?


Do you mean the global JNDI name?

If field or the method request injection of a resource of a different (non-default) name,Then How can one differentiate b/w the JNDI name for method and field (In below 2 statements,it seems JNDI name doesn't changes with change in a different resource injection)?


I guess that they mean here that if you use a non-default name you can still only inject it in either a field or a setter but not in both.

Regards,
Frits
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not taking about global JNDI name.My Query is

Q1:why is the following JNDI name created for the field of Bean class/bean methods to which a resource is injected instead of creating JNDI name for the actual resource

JNDI name:
Suppose a String of value="test" is injected to a field of a Bean class.
Q2:Then can't there be a JNDI name something like:

JNDI name:

I guess that they mean here that if you use a non-default name you can still only inject it in either a field or a setter but not in both



Frits,Can you please provide a example showing the same.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please advise
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why is the following JNDI name created for the field of Bean class/bean methods to which a resource is injected instead of creating JNDI name for the actual resource


That is just specified, but you can still have JNDI name of "java:comp/env/test".

Example:


Frits,Can you please provide a example showing the same.


You are not allowed to inject a field and a settter at the same time:

Note that Glassfish allows this, but the specs don't.

When having a non-default name (e.g. initNumber) you can also not inject both the property and the setter:
However Glassfish allows this.

To make it even stranger Glassfish also allows this:

and:

When you print the injected integer the value is: 10 (so the setter injection is either overruled or ignored by the property injection)

Regards,
Frits
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Frits
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic