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

前端 未结 2 1135
遇见更好的自我
遇见更好的自我 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
    0 讨论(0)
  • 2020-12-24 07:37

    @Inject annotation is used for java beans(POJOs) while @EJB annotation is used for enterprise java beans. When a container inject an ejb provided by @EJB annotation to another bean it also controls that ejb's lifecycle, performs pooling for stateless beans and so on(when bean which is going to be injected is not deployed the reference will be null). If you use @Inject annotation CDI mechanism simply find and create an instance of injected resource like with new operator(if the implementation of interface which is going to be injected doesn't exist the reference will be null). You can use qualifiers with @Inject annotation to choose different implementation of injected interface.

    0 讨论(0)
提交回复
热议问题