【问题标题】:Liberty profile not injecting ejb in servletLiberty 配置文件未在 servlet 中注入 ejb
【发布时间】:2015-07-29 07:32:01
【问题描述】:

我在谷歌上搜索了一下,试图理解为什么 Liberty Profile 8.5.5.6 没有将 EJB 注入到 servlet 中

我把一切都投入了一场战争。战争部署得很好,但是当我调用 servlet 时出现以下错误:

Exception thrown by application class 'org.javaee7.servlet.simple.UserServlet.doGet:23'
java.lang.NullPointerException:
at org.javaee7.servlet.simple.UserServlet.doGet(UserServlet.java:23)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1285)
at [internal classes]

this 链接上,我看到一条评论说我应该使用@ManagedBean

当我尝试这个时,空指针异常消失了,但现在我得到了这个错误:

Error 404: javax.servlet.UnavailableException: SRVE0319E: For the [UserServlet] servlet, org.javaee7.servlet.simple.UserServlet servlet class was found, but a resource injection failure has occurred. CWNEN0030E: The server was unable to obtain an object instance for the java:comp/env/org.javaee7.servlet.simple.UserServlet/service reference. The exception message was: The EJB reference in the lps.war module of the lps application could not be resolved; nested exception is: com.ibm.ejs.container.EJBNotFoundException: EJB with interface org.javaee7.service.UserService not present in application lps. 

这是我的代码:

@Stateless
public class UserServiceImpl implements UserService {
    public User getUser(Long id) {
        return new User(id, "Gary");
    }
}

@Remote
public interface UserService {
    public User getUser(Long id);
}

@ManagedBean
public class UserServlet extends HttpServlet {
    @EJB(mappedName = "UserServiceImpl")
    UserService service;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = service.getUser(1L);
        response.getWriter().print(user == null ? "no user" : user.toString());
    }
}

【问题讨论】:

    标签: java jakarta-ee servlets websphere-liberty


    【解决方案1】:

    首先 - 你肯定不应该使用@ManagedBean。 Servlet 不是托管 bean。您不需要它来进行注射。 您应该在 servlet 中包含 @WebServlet 或通过 web.xml 配置它。
    其次 - 你应该删除 mappedName 因为 Liberty 没有使用它。

    请在您的问题中添加server.xml,因为您可能缺少某些功能。
    您应该在那里拥有ejbRemote-3.2javaee-7.0 功能。

    对于远程接口,您必须使用以下绑定:

    @EJB(lookup="java:global/AppName/ModuleName/UserServiceImpl!org.javaee7.service.UserService")
    private UserService service;
    

    您还可以根据您的配置使用以下其他绑定:

    java:global/ExampleApp/ExampleModule/ExampleBean!com.ibm.example.ExampleRemoteInterface    
    java:app/ExampleModule/ExampleBean!com.ibm.example.ExampleRemoteInterface    
    java:module/ExampleBean!com.ibm.example.ExampleRemoteInterface
    

    当您的 bean 被绑定时,您应该在日志中看到以下消息:

    [INFO    ] CNTR0167I: The server is binding the com.ibm.example.ExampleRemoteInterface interface of the ExampleRemote enterprise bean in the Example.war module of the Example application.  
    The binding location is: java:global/Example/ExampleRemote!com.ibm.example.ExampleRemoteInterface
    

    更新
    如果是相同的应用程序,只需 @EJB 没有任何查找也适用于我。

    更多详情请参见:

    【讨论】:

    • 当我部署一个名为 lps.war 的 war 文件时,AppName 和 ModuleName 会是什么
    • @javapenguin 如果只有一场战争,它应该只是模块名称,所以java:global/lps/UserServiceImpl!org.javaee7.service.UserService
    • 我一删除 web.xml,它就起作用了。看起来如果您使用注释 (@WebServlet),您应该坚持使用它们。
    猜你喜欢
    • 2014-04-25
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多