Injection of HttpServletRequest

后端 未结 2 1939
鱼传尺愫
鱼传尺愫 2020-12-17 16:56

I am using ejb 3 and trying to @Inject HttpServletRequest, but while deploying I occur exception.

Code:

@Inject private HttpServletRequest httpReques         


        
2条回答
  •  北海茫月
    2020-12-17 17:34

    The lifecycle of HttpServletRequest is managed by the EJB/web container, not the CDI container. Attempting to inject it leads to issues because there are typically many implementations of the interface,and your CDI container does not have enough information to make a decision on which implementation to inject. Even if you successfully injected an instance of it, it would not be the same instance as being managed by the EJB container.

    To acquire a properly managed instance of the request, do this instead:

    @Context
    private HttpServletRequest httpRequest;
    

提交回复
热议问题