HK2 没有用球衣注入 HttpServletRequest球衣、HttpServletRequest

2023-09-06 10:50:43 作者:想贏別喊停#

我正在尝试按照位于 这里的示例在为了注入我的HttpSession.不幸的是,无论我尝试什么,它都不起作用.不知道可能是什么问题.

I am trying to follow the example located here to create a factory in order to inject my HttpSession. Unfortunately no matter what I try it is not working. Not sure what could be the issue.

我尝试只注入 HttpServletRequest 和提供程序.这是我使用提供程序的示例.尝试在提供方法中访问提供程序时,错误是空指针异常.如果我尝试注入 HttpServletRequest,则没有可用于注入的对象.我正在使用 JerseyTest 在 GrizzlyTestContainer 中运行它.为了绑定 HttpServletRequest,我需要添加一些东西到我的活页夹中吗?我似乎找不到一个例子.

I have tried injecting just the HttpServletRequest and a provider. Here is my example using a provider. The error is a null pointer exception when trying to access the provider in the provide method. If I try to inject the HttpServletRequest I get no object available for injection. I am running this inside the GrizzlyTestContainer using JerseyTest. Is there something I need to add to my binder in order to bind the HttpServletRequest? I cannot seem to find an example.

public class HttpSessionFactory implements Factory<HttpSession> {

    private final HttpServletRequest request;

    @Inject
    public HttpSessionFactory(Provider<HttpServletRequest> requestProvider) {
        this.request = requestProvider.get();
    }

    @Override
    public HttpSession provide() {
       return request.getSession();
    }

    @Override
    public void dispose(HttpSession t) {
    }
}

推荐答案

你应该在 JerseyTest 中的 @Override protected DeploymentContext configureDeployment() 返回一个