Liberty 配置文件未在 servlet 中注入 ejb
Liberty profile not injecting ejb in servlet
我用谷歌搜索试图理解为什么 Liberty Profile 8.5.5.6 没有将 EJB 注入 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 link 我看到一条评论说我应该使用 @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。你不需要它来注射工作。
您应该在您的 servlet 中包含 @WebServlet
或通过 web.xml
.
配置它
其次 - 你应该删除 mappedName
因为 Liberty 没有使用它。
请将 server.xml
添加到您的问题中,因为您可能缺少某些功能。
您应该在那里有 ejbRemote-3.2
或 javaee-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
而无需任何查找也适用于我。
另请参阅:
我用谷歌搜索试图理解为什么 Liberty Profile 8.5.5.6 没有将 EJB 注入 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 link 我看到一条评论说我应该使用 @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。你不需要它来注射工作。
您应该在您的 servlet 中包含 @WebServlet
或通过 web.xml
.
配置它
其次 - 你应该删除 mappedName
因为 Liberty 没有使用它。
请将 server.xml
添加到您的问题中,因为您可能缺少某些功能。
您应该在那里有 ejbRemote-3.2
或 javaee-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
而无需任何查找也适用于我。
另请参阅: