如何将无状态会话bean注入servlet



我的高级目标是使用NetBeans生成的JPA代码,方法是在servlet中使用创建"来自数据库的RESTful web服务"向导。

更准确地说,我想直接从servlet访问facade,以避免在客户端使用JavaScript加载一些数据。

我的外表的相关部分看起来是这样的:

@Stateless
@Path("wgm.rest.balanceview")
public class BalanceViewFacadeREST extends AbstractFacade<BalanceView> {
  @PersistenceContext(unitName = "WGManagerPU")
  private EntityManager em;
  @GET
  @Override
  @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  public List<BalanceView> findAll() {
    return super.findAll();
  }
}

现在我尝试的是:

@WebServlet(name = "BalanceServlet", urlPatterns = "/balance/*")
public class BalanceServlet extends HttpServlet {
   @Inject
   private BalanceViewFacadeREST balanceFacade;

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
  ServletException, IOException {
    List<BalanceView> balances = balanceFacade.findAll();
    // ...
  }
}

然而,当部署到GlassFish时,我得到以下异常:

java.lang.RuntimeException: Unable to load the EJB module. DeploymentContext does not contain any EJB. Check the archive to ensure correct packaging for /home/severin/Projects/WGManager/build/web.
If you use EJB component annotations to define the EJB, and an ejb or web deployment descriptor is also used, please make sure that the deployment descriptor references a Java EE 5 or higher version schema, and that the metadata-complete attribute is not set to true, so the component annotations can be processed as expected

这听起来好像注入器找不到BalanceViewFacadeREST。我错过了什么?

我假设Servlet和EJB是本地的。我的假设是,EJB没有远程接口。

如果Servlet和EJB位于同一个容器中,那么如果容器中有Context和Dependency注入,则可以使用@EJB或@Inject进行anostate。

由于您既没有呈现REMOTE也没有呈现LOCAL接口,所以EJB属于"无接口"类型。这意味着您应该使用@LocalBean注释EJB

@Stateless
@LocalBean
@Path("wgm.rest.balanceview")
public class BalanceViewFacadeREST

 //@Inject
    OR
 // @EJB
  private BalanceViewFacadeREST balanceFacade;

最新更新