注入HttpServletRequest [英] Injection of HttpServletRequest
问题描述
我正在使用ejb 3并试图@Inject HttpServletRequest,但在部署时我发生异常。
I am using ejb 3 and trying to @Inject HttpServletRequest, but while deploying I occur exception.
代码:
@Inject private HttpServletRequest httpRequest;
异常:
org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [HttpServletRequest] with qualifiers [@Default] at injection point [[field] @Inject private com.kmware.ttk.highway.beans.session.UserSessionBean.httpRequest]
我该怎么办?
推荐答案
HttpServletRequest的生命周期由EJB / web容器管理,而不是CDI容器。尝试注入它会导致问题,因为通常有许多接口的实现,并且您的CDI容器没有足够的信息来决定要注入的实现。即使你成功地注入了一个实例,那么 不是 是与EJB容器一样管理的实例。
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;
这篇关于注入HttpServletRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!