Skip to main content

Question about Session beans local interfaces

4 replies [Last post]
magf
Offline
Joined: 2006-06-07

Hi,
I'm new to EJB3 and my question is:
Is there a way to inject a session bean in a POJO? I mean a POJO that is part of the same ejb module?

Thanks,
Mag

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ksak
Offline
Joined: 2005-05-20

No, in Java EE 5, injection is only supported for certain managed classes such as ejb bean classes, interceptor classes, servlets, JSF managed beans, etc. However, other helper code running within a component invocation can still access the component's environment via java:comp/env. One exception to that is the new Java Persistence API entities. If those are the POJO's you would like to look up EJBs from, that is not recommended nor is it portable.

Assuming ejbs ABean and BBean, with helper class AUtil that is called from ABean, you could declare an ejb dependency in ABean as follows :

@EJB(name="BRef", beanInterface=B)
public class ABean implements A {

public void foo() {
AUtil autil = new AUtil();
autil.foo();
}
...

Then in Autil, look up the ejb dependency :

B b = (B) new InitialContext().lookup("java:comp/env/BRef");

--ken

magf
Offline
Joined: 2006-06-07

Thank you Ken very much...
In your example is B bean accessed using its local interface?

ksak
Offline
Joined: 2005-05-20

The normal rules apply. You can always access the Remote interface of a target bean from an ejb component. For portable Local access, the target bean must be packaged either in the same ejb-jar or in a different ejb-jar within the same .ear.

magf
Offline
Joined: 2006-06-07

I think I could not find a good answer. How I can access a session bean local interface from a helper class? (NOT from a remote client or a web tier)? Any help is appreciated...
Thanks
Mag