- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经在我的资源类中使用 @Context
成功注入(inject)了 HttpServletRequest
。
是否可以将 HttpServletRequest
注入(inject)到非资源类中?我创建了一个类来执行各种网络操作,这些操作需要将值存储在 HttpSession
中。例如……
public class RequestExecutor {
@Context
HttpServletRequest request;
public Response performNetworkRequest(Request request) {
// Do network request - I want to access the session without passing the session object around my code everywhere.
return response;
}
}
这可能吗?
最佳答案
你不能使用@Context
,见JSR-311对于 JAX-RS 1.1 和 JSR-339对于 JAX-RS 2.0:
JAX-RS provides facilities for obtaining and processing information about the application deployment context and the context of individual requests. Such information is available to
Application
subclasses (see section 2.1), root resource classes (see chapter 3), and providers (see chapter 4).
你也可以初始化子资源:
The
ResourceContext
interface provides access to instantiation and initialization of resource or subresource classes in the default per-request scope. It can be injected to help with creation and initialization, or just initialization, of instances created by an application.
另请参阅:ResourceContext#initResource
但是你可以使用继承:
public abstract class AbstractResource {
@Context
HttpServletRequest request;
protected Response performNetworkRequest() {
// do something with request
}
}
@Path("myResource")
public class MyResource extends AbstractResource {
// some methods
}
关于java - 如何将 HttpServletRequest 注入(inject)非资源类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37379215/
我是一名优秀的程序员,十分优秀!