@ Inject,@ EJB,@ Local,@ Remote,@ LocalBean等......:困惑? [英] @Inject, @EJB, @Local, @Remote, @LocalBean, etc... : confused?

查看:130
本文介绍了@ Inject,@ EJB,@ Local,@ Remote,@ LocalBean等......:困惑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下配置:


  • 1个包含2个包含EJB组件的EJB-JAR的GF上的EAR。

  • 1个包含访问EJB组件的Web组件的Glassfish服务器( =>其他JVM )上的WAR。

  • 1 EAR on one GF containing 2 EJB-JARs with EJB components.
  • 1 WAR on another Glassfish server (=> other JVM) containing web components accessing the EJB components.

我的EAR的每个EJB-JAR中都有2个EJB业务服务,它们都是这样开发的:

I have 2 EJB business services in each EJB-JAR of my EAR, and they are all developped like this:

@Remote
public interface ServiceAItf {
    ...
}

@Stateless
@Local
public class ServiceAImpl implements ServiceAItf {
    ...
}

在我的WAR中,我访问EJB组件通过远程接口上的显式InitialContext.lookup

In my WAR, I access the EJB components via an explicit "InitialContext.lookup" on the remote interface.

在我的EAR中,我对注射的最佳实践感到困惑,在性能,架构等方面...

In my EAR, I am quite confused about the best practice for injection, in terms of performance, architecture, and so on...

我有以下问题:


  • 如您所见,我已经声明了注释@关于服务实现的本地,无需定义本地接口。这是对的吗?至少我在部署时没有错误。但也许我应该使用@ LocalBean注释?我想@ LocalBean注释只是允许直接作为Local EJB调用实现,但你必须在代码中使用这样的实现:

  • As you can see, I have declared the annotation "@Local" 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 "@LocalBean" annotation instead? I suppose that the "@LocalBean" annotation simply allows to invoke the implementation directly as a "Local" EJB, but you have to use the implementation in your code like this:

@Stateless
@Local
公共类ServiceBImpl实现ServiceBItf {
@EJB
私有ServiceAImpl serviceA;
...
}

@Stateless @Local public class ServiceBImpl implements ServiceBItf { @EJB private ServiceAImpl serviceA; ... }

将一个EJB注入另一个EJB的最佳方法是什么?
它是这样的:

What is the best way to inject one EJB into another one? It works like this:

@Stateless
@Local
公共类ServiceBImpl实现ServiceBItf {
@EJB
private ServiceAItf serviceA;
...
}

@Stateless @Local public class ServiceBImpl implements ServiceBItf { @EJB private ServiceAItf serviceA; ... }

但是从我注意到的是,serviceA注入了是远程代理,而它位于同一个EAR文件中的同一个JVM中。所以我认为会对性能产生影响。这就是为什么我试图注入这样的服务:

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:

@Stateless
@Local
public class ServiceBImpl implements ServiceBItf {
    @Inject
    private ServiceAItf serviceA;
    ...
}

但它不适用于GF,我有以下例外:

But it doesn't work in GF, I have the following exception:

WELD-001408 Unsatisfied dependencies for type [...] ...

然后我尝试创建一个本地接口,并且通过注释@Inject注入两个服务时都能正常工作

I then tried to create a local interface, and the injection via the annotation "@Inject" works when both services

即使我创建这样的本地接口,服务也不会通过注释@ Inject注入,但是为null:

Even if I create a local interface like this, the service is not injected via the annotation "@Inject" but is null:

@Local
public interface ServiceALocalItf {
    ...
}

我读了很多文章,强烈建议使用@ Inject而不是 @EJB用于本地调用。这引出了以下问题:在这种情况下,建议(或简单地使用)@ Local EJB调用?

I read a lot of articles where it is highly recommended to use "@Inject" instead of "@EJB" when it is for a local invocation. That leads me to the following question: in which case the "@Local" EJB invocation is recommended (or simply used)?

毕竟在此分析中,我得出以下结论:

After all this analysis, I come to the following conclusion:


  • 对于每项服务,我创建一个@ Local@ Remote接口。

  • 从WAR到EAR的EJB-JAR,对远程接口进行JNDI查找。

  • 从EJB-JAR到EJB-JAR,通过@ EJB注入本地接口。

  • 对于两个服务相同的EJB-JAR,通过@Inject注入本地接口。

  • For each service, I create a "@Local" and a "@Remote" interface.
  • From WAR to EJB-JAR of EAR, make a JNDI lookup to the remote interface.
  • From EJB-JAR to EJB-JAR, make an injection via "@EJB" to the local interface.
  • For two services within the same EJB-JAR, make an injection via "@Inject" to the local interface.

您如何看待它?这是正确的吗?

What do you think about it? Is it correct?

推荐答案


如你所见,我已经宣布了注释@Local在没有定义本地接口的服务
实现上。它是否正确?

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

使用EJB 3.1,删除了对本地接口的要求。除非你明确需要它们,否则无需编写它们。

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


将一个EJB注入另一个EJB的最佳方法是什么?

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

这里要写的东西:

使用Java EE 6,Java Enterprise已经改变。新的JSR定义了一个所谓的托管bean (不要与JSF托管bean混淆)作为一种最小组件,在依赖注入和生命周期管理方面仍然可以从容器中受益。这意味着:如果您有一个组件并且只是想要使用DI并让容器控制其生命周期,那么您不需要 来使用它。如果 - 并且仅当 - 您明确需要EJB功能(如事务处理,池化,钝化和群集),您最终将使用EJB。

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. 使用@Inject over @EJB,CDI(a)的概念适用于所有托管的
    bean(包括EJB)和(b)是有状态的,因此比纯@EJB DI优于

  2. 你确定你的
    组件需要EJB吗?

  3. 看看CDI
    绝对值得文档

  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

这篇关于@ Inject,@ EJB,@ Local,@ Remote,@ LocalBean等......:困惑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆