I am a little bit confused by the variety of the ways to inject an EJB and therefore I would be grateful to anyone has an explanation to one or more of the following themes:
1) What is the difference when injecting an EJB through annotations and specifying "name" or "lookup". Example:
@EJB(name="ejb/mybean") // or @EJB(lookup="ejb/mybean")
@Stateless
public class MyStateless implements BusinessLocalInterface{
...
}
I think "name" serves the lookup under "java:comp/env/...", but what is to be done differntly using "lookup"?
2) Doing the injection in ejb-jar.xml instead, i.e. :
<ejb-local-ref>
<ejb-ref-name>ejb/MyBean</ejb-ref-name>
<local>pl.LocalInterface</local>
<ejb-link>BeanClass</ejb-link>
<injection-target>
<injection-target-class>pl.MyClass</injection-target-class>
<injection-target-name>targetName</injection-target-name>
</injection-target>
</ejb-local-ref>
What is the diferrence between <ejb-local-ref> and <ejb-ref>?
What is the injection-target elements, what can one achive with them and when are they to be used?
You do use the @EJB annotation in two scenarios.
In your application when you want to inject the bean
@EJB
MyStateless myStateless
In this second scenario you then also might use the lookup parameter to define the JNDI path to your EJB you want to inject. This is mainly used for example if your EJB is a remote interface in a different EAR or even different application server
@EJB(lookup = "ejb:otherEar/modulename/MyStateless!com.yourcomp.MyStateless")
MyStateless myStateless
3.) Some remark about CDI: @EJB is older than @Inject and offers more and EJB specific parameters, though as long as you only use EJBs as simple local services, try to use @Inject as you then are able to use other features like producers etc. Sometime one does not need an EJB at all :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With