java 注入 HttpServletRequest
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13419887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Injection of HttpServletRequest
提问by Kirill Bazarov
I am using ejb 3 and trying to @Inject HttpServletRequest, but while deploying I occur exception.
我正在使用 ejb 3 并尝试 @Inject HttpServletRequest,但在部署时出现异常。
Code:
代码:
@Inject private HttpServletRequest httpRequest;
Exception:
例外:
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]
What could I do with that?
我能用它做什么?
回答by Perception
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 notbe the same instance as being managed by the EJB container.
HttpServletRequest 的生命周期由 EJB/web 容器管理,而不是 CDI 容器。尝试注入它会导致问题,因为接口通常有很多实现,而您的 CDI 容器没有足够的信息来决定注入哪个实现。即使您成功注入了它的一个实例,它也不会与 EJB 容器管理的实例相同。
To acquire a properly managed instance of the request, do this instead:
要获取正确管理的请求实例,请改为执行以下操作:
@Context
private HttpServletRequest httpRequest;
回答by Murray Todd Williams
If your dependent is a JAX-RS (Restful) class then note the answer above. On the other hand, if you've got a more complex arrangement of dependency injection the question is certainly valid.
如果您的家属是 JAX-RS (Restful) 类,请注意上面的答案。另一方面,如果您有更复杂的依赖注入安排,那么这个问题肯定是有效的。
This capability has been added to the CDI 1.1 specification (JSR-346) which in turn has been added to the new Java EE 7 specification. In other words, the newest class of Java Enterprise Application servers will be able to handle this.
此功能已添加到 CDI 1.1 规范 (JSR-346) 中,而后者又已添加到新的 Java EE 7 规范中。换句话说,最新的 Java 企业应用服务器类将能够处理这个问题。
In the meantime, if you need to be able to manage some request scopes dependencies that in term need access to the actual HttpServletRequest information, you can use your approach and use the JBoss Soldertool. (Don't panic if the web site looks defunct, the fact of the matter is the work got shifted to the official CDI 1.1 spec implementation—i.e. "Weld 2"—so they aren't working on solder any more. But it's still perfectly suitable for CDI 1.0.)
同时,如果您需要能够管理某些请求范围依赖项,而这些依赖项实际上需要访问实际的 HttpServletRequest 信息,您可以使用您的方法并使用JBoss Solder工具。(如果该网站看起来已不复存在,请不要惊慌,事实是工作已转移到官方 CDI 1.1 规范实现——即“焊接 2”——所以他们不再从事焊接工作。但它仍然非常适合 CDI 1.0。)
The maven dependencies would be
Maven 依赖项将是
<dependency>
<groupId>org.jboss.solder</groupId>
<artifactId>solder-api</artifactId>
<version>3.2.0.Final</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jboss.solder</groupId>
<artifactId>solder-impl</artifactId>
<version>3.2.0.Final</version>
<type>jar</type>
<scope>compile</scope>
</dependency>