gpt4 book ai didi

java - Liberty 配置文件未在 servlet 中注入(inject) ejb

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:35 24 4
gpt4 key购买 nike

我用谷歌搜索试图理解为什么 Liberty Profile 8.5.5.6 没有将 EJB 注入(inject) servlet

我把一切都投入了 war 。 war 部署正常,但是当我调用 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());
}
}

最佳答案

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

请将 server.xml 添加到您的问题中,因为您可能缺少某些功能。
您应该具有 ejbRemote-3.2javaee-7.0 功能等。

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

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

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

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 绑定(bind)时,您应该在日志中看到以下消息:

[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 而不进行任何查找也适用于我。

另请参阅更多详细信息:

关于java - Liberty 配置文件未在 servlet 中注入(inject) ejb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31693982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com