@Inject, @EJB, @Local, @Remote, @LocalBean, etc… : confused?

前端 未结 2 1140
遇见更好的自我
遇见更好的自我 2020-12-24 07:06

I have the following configuration:

  • 1 EAR on one GF containing 2 EJB-JARs with EJB components.
  • 1 WAR on another Glassfish server (=> other JVM)
2条回答
  •  庸人自扰
    2020-12-24 07:23

    As you can see, I have declared the annotation "@Local" on the service implementation without defining the local interface. Is it correct?

    With EJB 3.1, the requirement for local interfaces was dropped. No need to write them unless you explicitly need them.

    What is the best way to inject one EJB into another one?

    Couple of things to write here:

    With Java EE 6, Java Enterprise has changed. A new JSR defines a so-called managed bean (don't confuse with JSF managed beans) as a sort of minimum component that can still benefit from the container in terms of dependency injection and lifecycle management. This means: If you have a component and "just" want to use DI and let the container control its lifecycle, you do not need to use EJBs for it. You'll end up using EJBs if - and only if - you explicitly need EJB functionality like transaction handling, pooling, passivation and clustering.

    This makes the answer to your question come in three parts:

    1. Use @Inject over @EJB, the concept of CDI (a) works for all managed beans (this includes EJBs) and (b) is stateful and therefore far superior over pure @EJB DI
    2. Are you sure that you need EJBs for your components?
    3. It's definitely worthwhile to have a look at the CDI documentation

提交回复
热议问题