简体   繁体   English

我怎样才能 select EJB 的本地接口?

[英]How can I select the local interface for an EJB?

Say I have the following EJB (using ejb3):假设我有以下 EJB(使用 ejb3):

@Stateless(name="Queries")
@Remote(Queries.class)
@Local(Queries.class)
public final class QueriesEJB implements Queries {
    ...
}

The class is available through both a local and a remote interface. class 可通过本地和远程接口使用。

How can I inject the local interface for this EJB in another part of the app?如何在应用程序的另一部分注入此 EJB 的本地接口?

Specifically, I'm not sure how to create an @EJB annotation that selects the local interface.具体来说,我不确定如何创建选择本地接口的@EJB 注释。 For example, is the following sufficient?例如,以下内容是否足够?

@EJB(name="Queries") private Queries queries;

In particular I want to avoid creating separate local and remote interfaces simply for the purpose of distinguishing via @EJB's 'beanInterface' property.特别是,我想避免仅仅为了通过@EJB 的“beanInterface”属性进行区分而创建单独的本地和远程接口。

According to the spec you cannot have an interface that is Remote and Local at the same time.根据规范,您不能同时拥有远程和本地接口。 However, you create a super-interface, put all methods there, and then create 2 sub-interfaces.但是,您创建了一个超级接口,将所有方法放在那里,然后创建 2 个子接口。 Having done that, simply use @EJB.完成后,只需使用@EJB。 This way you need to maintain only one interface at all.这样,您根本只需要维护一个接口。

EDIT: See section 3.2 in "EJB3 spec simplified" at http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html编辑:请参阅http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html的“EJB3 规范简化”中的第 3.2 节

When a EJB is deployed, the container looks at the interfaces and identifies local and remote interfaces.部署 EJB 时,容器查看接口并识别本地和远程接口。 I would say that the EJB container already uses the local interface in your example.我会说 EJB 容器已经在您的示例中使用了本地接口。 It simply does not make sence to use a remote interface in this case because the container has the choice to use the local one.在这种情况下使用远程接口根本没有意义,因为容器可以选择使用本地接口。

If you want to be sure try to use the JNDI name of the local interface as parameter of the @EJB annotation.如果您想确保尝试使用本地接口的 JNDI 名称作为 @EJB 注释的参数。

@EJB(name="java:comp/env/ejb/EntitySupplierLocal")

In the example above I added local to the interface name.在上面的示例中,我在接口名称中添加了 local。 In your case you have to take a look at the JNDI context to get the right name or you even know it;).在您的情况下,您必须查看 JNDI 上下文以获得正确的名称,或者您甚至知道它;)。

Generally I recommend to use a base interface that has the business methodes defined and extend a local and a remote interface.通常,我建议使用定义了业务方法的基本接口并扩展本地和远程接口。 So you do not have to duplicate methodes and you are able to extend functionality for local and remote access seperatly.因此,您不必复制方法,并且可以分别扩展本地和远程访问的功能。

public interface Queries () { .. }

@Local
public interface QueriesLocal extends Queries () { .. }

@Remote
public interface QueriesRemote extends Queries () { .. }

Solutions from previous comments are not fully compatibile with EJB 3.0 on Jboss.先前评论中的解决方案与 Jboss 上的 EJB 3.0 不完全兼容。

You can easy get this error:你很容易得到这个错误:

org.jboss.ejb3.common.resolvers.spi.NonDeterministicInterfaceException:
beanInterface specified, Queries, is not unique within EJB QueriesEJB

Create only this:只创建这个:

public interface Queries ()  //Local by default

@Remote
public interface QueriesRemote extends Queries () { ... }

It works有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM