EJB 中的本地/远程和无接口视图是什么?
我试图理解 EJB 中不同客户端视图的目的和原因。有人可以尝试解释一下吗?
I am trying to understand what the purpose and why we need the different client views in EJB. Could someone try to explain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
远程客户端视图
当您的 EJB 及其客户端将处于分布式环境中时 - 这意味着 EJB 和客户端将驻留在单独的 Java 虚拟机上。示例:托管在 WebSphere Application Server 上的 EJB 和使用 Tomcat 服务器上托管的 EJB API 的 Servlet。
本地客户端视图
仅当保证其他企业 Bean 或客户端仅在单个 JVM 内寻址该 Bean 时。例如,EJB 以及 Servlet 部署在同一 WebSphere 服务器上。
无界面视图
与本地客户端视图几乎相同,但也有差异。在这种情况下,您的 bean 类不需要实现客户端视图接口。 bean 类的所有公共方法都会自动暴露给调用者。无接口视图始终获取 EJB 引用 - 就像本地或远程视图一样 - 通过注入或 JNDI 查找;但是,EJB引用的Java类型是bean类类型而不是本地接口类型。这是作为 Java EE6 的一部分引入的一项便利功能。
本地客户端视图和无接口视图的区别
在无接口视图的情况下,客户端和目标bean必须打包在同一个应用程序(EAR)中。在本地视图的情况下,客户端可以打包在与企业应用程序不同的单独应用程序中。因此,这在细粒度组件方面提供了更大的灵活性。
您可以根据您的 API 使用场景使用本地客户端视图还是无接口视图。无界面视图很可能在未来的规范中获得灵活的功能。
原因
历史上或其他方面,希望使用 EJB 服务的客户端应该“查找”容器上的 bean(具有某些初始上下文)。这是因为所有调用都是通过容器提供的特殊 EJB 引用(代理)进行的。这允许容器提供所有附加的 bean 服务,例如池、容器管理的事务等。因此,客户端无法使用 new 运算符显式实例化 EJB。客户端视图是通过客户端可以访问的某些接口提供的。服务器端的代理实现就是基于这些接口来完成的。如上所述,定义了不同的客户端视图以适应不同的部署场景。
Remote client view
When your EJB and its clients will be in a distributed environment - meaning EJBs and clients will reside on separate Java virtual machines. Example : EJBs hosted on a WebSphere Application Server and Servlets that consume EJB APIs hosted on a Tomcat server.
Local client view
Only when it is guaranteed that other enterprise beans or clients will only address the bean within a single JVM. Example, EJBs as well as the Servlets deployed on the same WebSphere server.
No-Interface view
Is almost same as local client view, but there are differences. Your bean class is not required to implement client view interfaces in this case. All public methods of the bean class are automatically exposed to the caller. no-interface view always acquires an EJB reference - just like local or remote views - either through injection or JNDI lookup; but, Java type of the EJB reference is the bean class type rather than the type of a local interface. This is a convenience introduced as part of Java EE6.
Difference between local client view and no-interface view
In case of no-interface view, the client and the target bean must be packaged in the same application (EAR). In case of local view, client can be packaged in a separate application than the enterprise application. So, this gives more flexibility in terms of fine-graining your components.
You may use local client view vs no-interface view depending on your API usage scenario. It is very likely for no-interface view to receive flexible features in future specs.
Reason
Historically or otherwise, a client wishing to use EJB services was supposed to "look up" the bean on the container ( with certain initial contexts ). That was because all invocations are made through a special EJB reference(proxy) provided by the container. This allows the container to provide all the additional bean services such as pooling, container-managed transactions etc. So, a client can not explicitly instantiate an EJB with
new
operator. The client view is provided via certain interfaces that the client would have access to. The proxy realization at server side is done based on these interfaces. Different client views are defined to suite different deployment scenarios as mentioned above.根据 EJB 3.1 规范第 3.2.2 节:
As per Section 3.2.2 of EJB 3.1 Specification: