File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dependancy Injection when 2 Beans implement the same interface

 
Jarrod Felino
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 404
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use name attribute of @EJB
 
Ralph Jaus
Ranch Hand
Posts: 342
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Chaminda Amarasinghe
Ranch Hand
Posts: 404
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic