aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Dependancy Injection when 2 Beans implement the same interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Dependancy Injection when 2 Beans implement the same interface" Watch "Dependancy Injection when 2 Beans implement the same interface" New topic
Author

Dependancy Injection when 2 Beans implement the same interface

Jarrod Felino
Greenhorn

Joined: Nov 15, 2008
Posts: 26
How can I tell BeanA which implemetation of the interface I1 to inject. Can this be done with a field level annotation or must it be done at class level?

When I run JBoss I get a runtime error stating it could not resolve the global JNDI name for @EJB.

Please have a look at the following code:

Chaminda Amarasinghe
Ranch Hand

Joined: May 17, 2006
Posts: 402
use name attribute of @EJB
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
How can I tell BeanA which implemetation of the interface I1 to inject
Try

@EJB(beanName="BeanB1")
I1 m2;

Please let us know, if it works.
[ November 21, 2008: Message edited by: Ralph Jaus ]

SCJP 5 (98%) - SCBCD 5 (98%)
Chaminda Amarasinghe
Ranch Hand

Joined: May 17, 2006
Posts: 402
see this. it better to use name instead of beanname

see
What is the relationship between @EJB and ejb-ref/ejb-local-ref?
section in https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html

see also http://java.sun.com/javaee/5/docs/api/javax/ejb/EJB.html
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Cham,

thanks for the link to the glassfish site. I was googling for it some time ago but couldn't find it.

Maybe I'm blind, but I couldn't find any hint there, that it is better to use "name" instead of "beanName". Can you please quote.

I tried a similar sample like Jarrod and got the following problem:

- suppose the qualified name of I1 is "test.I1"

- when deploying BeanB1 the container generates a jndi reference with global jndi name "test.I1" for BeanB1.

- when deploying BeanB2 the container tries to generate a jndi reference with global jndi name "test.I1"

- since this jndi name already exists, the deployment fails.

When turning I1 into a local business interface and using

@EJB(beanName="BeanB1")
I1 m2;

everything works fine. Without beanName the deployment fails again because of the ambiguity which bean to choose.
Jarrod Felino
Greenhorn

Joined: Nov 15, 2008
Posts: 26
Thanks a lot. This makes sense now. I am just a bit confused now between beanName and name with dependency injection.

Take this class level annotation:

@EJB(name="ejb/test/whatever/BeanA", beanInterface=I1.class, beanName="BeanA")

Basically you can do a jndi lookup on "ejb/test/whatever/BeanA" and it will return the BeanA through the interface.


But what if I used the same annotation at a field level? Does name have any real use? Name seems to be more of an alias?
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Jarrod,

if you use

@EJB(name="xyz", beanInterface=I1.class, beanName="BeanB1")

on the class level of bean BeanA, the container will create a jndi entry for a BeanB1 object in the environment of BeanA with jndi name "java:comp/env/xyz".

If you're using it on field level (say for field m2), the container

- creates the same jndi entry in BeanA's environment
- does a jndi lookup when it creates an instance of BeanA
- assigns the obtained reference to m2

So the only difference between class level and field level is that on field level the container performs additionally the jndi lookup.

If you use

@EJB(beanName="BeanB1")
I1 m2;

the container will use as default jndi name "java:comp/env/test.BeanA/m2" (suposse "test.BeanA" is the qualified name of BanA). Everything else works as above.

That was the technical explanation. Actually I see no reason for using "name" on field level, since it's equal which jndi name the container creates and looks up there.

beanName is only needed in the very scarce situation, that two beans in your ejb-jar-file or ear-file implement the same local business interface.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dependancy Injection when 2 Beans implement the same interface