aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Questions on Enterprise Bean’s Environment as a JNDI Naming Context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Questions on Enterprise Bean’s Environment as a JNDI Naming Context" Watch "Questions on Enterprise Bean’s Environment as a JNDI Naming Context" New topic
Author

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

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

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.


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1715
    
  25

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

Joined: May 18, 2010
Posts: 634

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
Bartender

Joined: Apr 07, 2010
Posts: 1715
    
  25

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

Joined: May 18, 2010
Posts: 634

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

Joined: May 18, 2010
Posts: 634

Please advise
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1715
    
  25

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

Joined: May 18, 2010
Posts: 634

Thanks Frits
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Questions on Enterprise Bean’s Environment as a JNDI Naming Context