【问题标题】:EJB interface references from a separated Web Project来自分离的 Web 项目的 EJB 接口引用
【发布时间】:2017-05-22 15:29:04
【问题描述】:

我正在 Eclipse 上做一些 EJB 3.1 实践。

我在 Wildfly 9 上有一个 EJB 项目和一个动态 Web 项目。

我想要做的是调用 EJB 方法 (EJB 项目)来自 Servlet(在动态 Web 项目中) 在 Servlet 代码上使用 @EJB 注释:

@EJB
IBeanRemote bean; //realize the bean variable is declared as the remote EJB Interfase.

protected void doGet(HttpServletRequest request, HttpServletResponse    response) throws ServletException, IOException {    
    response.getWriter().println(bean.getMethod());
}

而且我的 EJB 非常简单:

@Remote
@Stateless
public interfase IBeanRemote {
   public String getMethod();
}
...
public BeanRemote extends IBeanRemote
{
    @Override
    public String getMethod() { 
        return "How to call this ejb?"; 
    }
}

两个项目都被打包到一个 EAR 项目中,但我不能 从 Web 项目中查看 IBeanRemote 类型引用(因为 IBeanRemote 和 Servlet 位于单独的项目中),所以,行

@EJB
IBeanRemote bean;

无法编译。

一个常见的解决方案是在 Web 项目构建路径中添加 EJB 项目,但如果我使用在非常远程的服务器上运行的远程 EJB 并且我认为 Web 项目不应该知道任何关于EJB 实现。

如何将 IRemoteBean 引用类型添加到我的 Web 项目中以创建我的 Servlet 类 在eclipse中编译并调用EJB方法?

【问题讨论】:

    标签: eclipse servlets jakarta-ee ejb wildfly


    【解决方案1】:

    为什么不将您的 EJB 项目分成 2 个较小的项目:包含接口的 EJB-API,您可以将这个项目用于您的客户端,以及实现接口的 EJB-IMPL。您的客户端和 EJB-IMPL 都将引用您的 EJB-API 项目。

    【讨论】:

    • 实际上我刚刚意识到,当我们在 EAR 中创建 EJB 项目时,通过选中此选项,我们可以在 eclipse 中自动创建一个 ejb 客户端 jar。只有 ejb 接口将被导出到客户端 jar。太好了,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 2015-04-24
    相关资源
    最近更新 更多