EJB 3.1 @LocalBean 与无注释 [英] EJB 3.1 @LocalBean vs no annotation
问题描述
我了解本地视图、远程视图和无接口视图之间的区别.我只是不明白无视图"(无注释)和无界面视图有什么区别.还有为什么我应该用 @Local
注释我的界面?如果我完全不注释接口,那有什么区别吗?
规则是(凭记忆):
- Bean 有一个
@LocalBean
注释 -> bean 有一个无接口视图 - Bean 有一个
@Local
注释 -> Bean 有一个本地视图 - Bean 有一个
@Remote
注释 -> Bean 有一个远程视图 - Bean 没有视图注解,而是直接实现了一个带有@Local 注解的接口 -> bean 有一个本地视图
- Bean没有视图注解,而是直接实现了一个带有@Remote注解的接口-> bean有一个远程视图
- Bean没有视图注解,而是直接实现了一个没有视图注解的接口-> bean有一个本地视图
- Bean 没有视图注解,也没有实现接口 -> bean 没有接口视图
因此,使用 @LocalBean
和根本不使用注释都是获得无接口视图的两种方式.如果你只是想要一个无接口的视图,那么最简单的事情就是不要注释.前提是您还没有实现任何接口.
@LocalBean
存在的部分原因是为了向也有接口视图的 bean 添加一个无接口视图.我想在规范作者的脑海中最重要的场景是你有一个像这样的 bean:
@Stateless公共类用户偏好{公共字符串 getPreference(字符串首选项名称);公共地图<字符串,字符串>获取首选项();}
您希望在本地公开两种方法,但仅远程公开粗粒度的 getPreferences()
.您可以通过仅使用该方法声明远程接口,然后在 bean 类上添加 @LocalBean
来实现.没有它,您就必须编写一个毫无意义的本地接口来在本地公开这两种方法.
或者,换个角度来看,@LocalBean
的存在是因为存在无接口视图这样的东西,而无注释选项作为一种方便的快捷方式存在.>
I understand the difference between local view, remote view and no-interface view. I just don't understand what is the difference between "no view" (no annotation) and no-interface view. And also why should I annotate my interface with @Local
? What if I don't annotate the interface in at all, is there a difference?
The rules are (from memory):
- Bean has a
@LocalBean
annotation -> bean has a no-interface view - Bean has a
@Local
annotation -> bean has a local view - Bean has a
@Remote
annotation -> bean has a remote view - Bean has no view annotations, but directly implements an interface which has a @Local annotation -> bean has a local view
- Bean has no view annotations, but directly implements an interface which has a @Remote annotation -> bean has a remote view
- Bean has no view annotations, but directly implements an interface which has no view annotations -> bean has a local view
- Bean has no view annotations, and implements no interfaces -> bean has a no-interface view
So, using @LocalBean
and using no annotation at all are both ways of getting a no-interface view. If you just want a no-interface view, then the simplest thing is not to annotate. Provided you're not also implementing any interfaces.
Part of the reason @LocalBean
exists to add a no-interface view to a bean which also has an interface view. I imagine the scenario uppermost in the spec authors' minds was one where you have a bean like:
@Stateless
public class UserPreferences {
public String getPreference(String preferenceName);
public Map<String, String> getPreferences();
}
Where you would want to expose both methods locally, but only the coarser-grained getPreferences()
remotely. You can do that by declaring a remote interface with just that method, then just slapping @LocalBean
on the bean class. Without it, you'd have to write a pointless local interface just to expose both methods locally.
Or, to look at it another way, the @LocalBean
exists because there is such a thing as a no-interface view, and the no-annotation option exists as a handy shortcut.
这篇关于EJB 3.1 @LocalBean 与无注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!