title: @Inject, @EJB, @Local, @Remote, @LocalBean, etc... : confused?
body: <p>I have the following configuration:</p>
  
  <ul>
  <li>1 EAR on one GF containing 2 EJB-JARs with EJB components.</li>
  <li>1 WAR on another Glassfish server (<i>=> other JVM</i>) containing web components accessing the EJB components.</li>
  </ul>
  
  <p>I have 2 EJB business services in each EJB-JAR of my EAR, and they are all developped like this:</p>
  
  <pre><code>@Remote
  public interface ServiceAItf {
      ...
  }
  
  @Stateless
  @Local
  public class ServiceAImpl implements ServiceAItf {
      ...
  }
  </code></pre>
  
  <p>In my WAR, I access the EJB components via an explicit <b>"InitialContext.lookup"</b> on the remote interface.</p>
  
  <p>In my EAR, I am quite confused about the best practice for injection, in terms of performance, architecture, and so on...</p>
  
  <p>I have the following questions:</p>
  
  <ul>
  <li><p>As you can see, I have declared the annotation <b>"@Local"</b> on the service implementation without defining the local interface. Is it correct? At least I have no error at deployment. But maybe I should use the <b>"@LocalBean"</b> annotation instead? I suppose that the <b>"@LocalBean"</b> annotation simply allows to invoke the implementation directly as a <b>"Local"</b> EJB, but you have to use the implementation in your code like this:</p>
  
  <p>@Stateless
  @Local
  public class ServiceBImpl implements ServiceBItf {
      @EJB
      private ServiceAImpl serviceA;
      ...
  }</p></li>
  <li><p><b>What is the best way to inject one EJB into another one?</b>
  It works like this:</p>
  
  <p>@Stateless
  @Local
  public class ServiceBImpl implements ServiceBItf {
      @EJB
      private ServiceAItf serviceA;
      ...
  }</p></li>
  </ul>
  
  <p>But from what I noticed, the "serviceA" injected is the remote proxy, while it is in the same JVM within the same EAR file. So I suppose that there will be an impact on the performance. That's why I have tried to inject the service like this:</p>
  
  <pre><code>@Stateless
  @Local
  public class ServiceBImpl implements ServiceBItf {
      @Inject
      private ServiceAItf serviceA;
      ...
  }
  </code></pre>
  
  <p>But it doesn't work in GF, I have the following exception:</p>
  
  <pre><code>WELD-001408 Unsatisfied dependencies for type [...] ...
  </code></pre>
  
  <p>I then tried to create a local interface, and the injection via the annotation "@Inject" works when both services </p>
  
  <p>Even if I create a local interface like this, the service is not injected via the annotation <b>"@Inject"</b> but is null:</p>
  
  <pre><code>@Local
  public interface ServiceALocalItf {
      ...
  }
  </code></pre>
  
  <p>I read a lot of articles where it is highly recommended to use <b>"@Inject"</b> instead of <b>"@EJB"</b> when it is for a local invocation. That leads me to the following question: in which case the <b>"@Local"</b> EJB invocation is recommended (or simply used)?</p>
  
  <p>After all this analysis, I come to the following conclusion:</p>
  
  <ul>
  <li>For each service, I create a <b>"@Local"</b> and a <b>"@Remote"</b> interface.</li>
  <li>From WAR to EJB-JAR of EAR, make a JNDI lookup to the remote interface.</li>
  <li>From EJB-JAR to EJB-JAR, make an injection via <b>"@EJB"</b> to the local interface.</li>
  <li>For two services within the same EJB-JAR, make an injection via "@Inject" to the local interface.</li>
  </ul>
  
  <p>What do you think about it? Is it correct?</p>