• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class-level dependency

 
Tang Yue
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the SCBCD5 Study Guide, there is an example of class-level dependency:

FooBean.java
@EJB(name="ejb/bar", beanInterface=Bar.class)

public class FooBean implements Foo {
...
}

Utility.java
Bar bar = (Bar) context.lookup("java:comp/env/ejb/bar");

I don't understand this example. If Utility.java needs a Bar, shouldn't it use @EJB in Utility.java? Why @EJB is used in FooBean.java? I don't see how these 2 java files are related at all.
 
Fu Dong Jia
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regards!
I think it should not be work at all!
 
Tang Yue
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess the book means inside FooBar there is call to a method in Utility.java
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tang Yue:
In the SCBCD5 Study Guide, there is an example of class-level dependency:

FooBean.java
@EJB(name="ejb/bar", beanInterface=Bar.class)

public class FooBean implements Foo {
...
}

Utility.java
Bar bar = (Bar) context.lookup("java:comp/env/ejb/bar");

I don't understand this example. If Utility.java needs a Bar, shouldn't it use @EJB in Utility.java? Why @EJB is used in FooBean.java? I don't see how these 2 java files are related at all.


Without looking at the code in the FooBean, i can only guess that the Utility.java class is being used in this bean. Going by the name of the "Utility" class, i guess its an unmanaged object - which means that you cannot use injection in this class. Effectively, you cannot use @EJB injection to get hold of the "Bar" bean. And that the reason why the Bar bean is being injected in the ENC of the FooBean so that the utility class can look it up from the ENC (java:comp/env namespace) of the FooBean.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic