EJB 3.1。是否需要@Local批注? [英] EJB 3.1. Is @Local annotation needed?

查看:63
本文介绍了EJB 3.1。是否需要@Local批注?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我几乎总是使用无接口EJB,并且对@Local注释的需要有一点了解。考虑以下示例:

So far, I almost always worked with no-interface EJBs and have a slight understanding about the need of @Local annotation. Consider this example:

public interface MyBeanIntf { void doStuff(); }

@Stateless
public class MyBean implements MyBeanIntf { public void doStuff(){ } }

是否应该将 MyBeanIntf​​ 标记为 @Local ?我看不到有任何好处,因为即使我没有将其注释为 @Local ,我仍然可以使用 DI 正确地将其注入到UI Controller中:

Should the MyBeanIntf be marked as @Local? I don't see any benefit from that, because even when I don't annotate it as @Local, I still can use DI to properly inject it into UI Controller:

@Named
@SessionScoped
public class TestController implements Serializable {

  // injection works perfectly, even when MyBeanIntf is not marked as @Local
  @Inject
  private MyBeanIntf myBean;

  // or even like this:
  // @EJB
  // private MyBeanIntf myBean;

}

让它变得更加复杂:

public interface MyBeanIntf { void doStuff(); }
public class MySuperBean implements MyBeanIntf { public void doStuff() { } }

@Stateless
public class MyBean extends MySuperBean { }

MyBean 现在被视为有效的 Local EJB 豆?我有一些疑问,因为它间接实现了接口。

Is MyBean now considered a valid Local EJB bean? I have some doubts because it implements the interface indirectly.

推荐答案

如果您的EJB实现了某些接口,但您未指定(两者都不在EJB上还是在接口本身上)(假设是@Local),它是哪个接口(@ Remote,@ Local)。

If your EJB implements some interface but you don't specify (neither on the EJB nor the interface itself) which interface it is (@Remote, @Local) than it's assumed that it's a @Local one.

因此您的代码:

public interface MyBeanIntf { void doStuff(); }

@Stateless
public class MyBean implements MyBeanIntf { public void doStuff(){ } }

的语义与以下内容相同:

is semantically identical to the following:

@Local
public interface MyBeanIntf { void doStuff(); }

@Stateless
public class MyBean implements MyBeanIntf { public void doStuff(){ } }

关于问题的第二部分,我认为EJB 3.1 FR规范的 4.9.2.1会话Bean超类对您来说很有趣。根据我的理解(因此可能不正确),由于以下摘录,似乎不应该将您的bean视为公开有效的Local接口:

When it comes to the second part of your question, I think that section 4.9.2.1 Session Bean Superclasses from EJB 3.1 FR spec would be interesting for you. From my understanding (so it might not be correct), it seems that your bean should not be considered as exposing a valid Local interface because of the following excerpt:

@Stateless
public class A implements Foo { ... }

@Stateless
public class B extends A implements Bar { ... }




假设Foo和Bar是本地业务接口,并且没有
相关的部署描述符中,会话Bean A公开本地
业务接口Foo,而会话Bean B公开本地业务
接口Bar,但不公开Foo

会话bean B需要在其
暴露视图集中显式包含Foo,以应用该接口。

Session bean B would need to explicitly include Foo in its set of exposed views for that interface to apply.

更新:

此外,该规范还摘录了以下内容:

As an addition one more excerpt from the spec:


会话bean类被允许具有超类本身就是
会话bean类。但是,在这种情况下,没有适用于注释处理或部署
描述符的特殊规则
。为了处理特定的
会话Bean类,所有父类的处理都是相同的,无论
是否超类本身就是会话Bean类。

这篇关于EJB 3.1。是否需要@Local批注?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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