泽西岛2-在过滤器中注入请求范围服务

时间:2019-07-31 08:28:08

标签: java jersey jersey-2.0 hk2

我正在编写RESTful Web服务,试图将请求范围的服务类注入过滤器中。 我已通过Paul Samsotha's blog讨论了如何使用代理注入请求范围的服务。

这是我的实现方式

供应商:

public class FileServiceSupplier implements Supplier<FileService> {

    @Inject
    ContainerRequestContext context;

    public FileService get() {
        String something = context.get("something");
        new FileService(something);
    }
}

我要在这里绑定供应商

import org.glassfish.jersey.internal.inject.AbstractBinder;
...
public class CustomDependencyBinder extends AbstractBinder {
    @Override
    protected void configure() {
        bindFactory(FileServiceSupplier.class)
            .proxy(true)
            .proxyForSameScope(false)
            .to(FileService.class)
            .in(RequestScoped.class);
    }
}

public class MyWebService extends ResourceConfig {
    public MyWebService() {
        register(new CustomDependencyBinder());
    }
}

但是现在当我将其注入过滤器时:

public class FileScanningFilter implements ContainerRequestFilter {

    @Inject
    private FileService fileService;

    @Inject
    private ResourceInfo resourceInfo;

    @Override
    public void filter(ContainerRequestContext containerRequestContext) throws IOException {
        boolean status = fileService.assertStatus();
    }
}

@Provider
public class FileScanningFeature implements DynamicFeature {
    @Override
    public void configure(ResourceInfo resourceInfo, FeatureContext featureContext) {
        featureContext.register(FileScanningFilter.class);
    }
}

但是现在当我发送请求时,出现以下错误:

"There is more than one active context for org.glassfish.jersey.process.internal.RequestScoped"  "java.lang.IllegalStateException: There is more than one active context for org.glassfish.jersey.process.internal.RequestScoped
    at org.jvnet.hk2.internal.ServiceLocatorImpl._resolveContext(ServiceLocatorImpl.java:2193)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.access$000(ServiceLocatorImpl.java:105)
    at org.jvnet.hk2.internal.ServiceLocatorImpl$3.compute(ServiceLocatorImpl.java:165)
    at org.jvnet.hk2.internal.ServiceLocatorImpl$3.compute(ServiceLocatorImpl.java:161)
    at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture$1.call(Cache.java:74)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture.run(Cache.java:131)
    at org.glassfish.hk2.utilities.cache.Cache.compute(Cache.java:176)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.resolveContext(ServiceLocatorImpl.java:2207)
    at org.jvnet.hk2.internal.MethodInterceptorImpl.internalInvoke(MethodInterceptorImpl.java:64)
    at org.jvnet.hk2.internal.MethodInterceptorImpl.invoke(MethodInterceptorImpl.java:101)
    at org.jvnet.hk2.internal.MethodInterceptorInvocationHandler.invoke(MethodInterceptorInvocationHandler.java:39)
    at com.sun.proxy.$Proxy88.getResourceMethod(Unknown Source)
    at com.test.FileScanningFilter.filter(AuthFilter.java:56)

即使我在过滤器中插入了一次,它也表示存在多个上下文。

我正在使用Jersey 2.28

P.S:对于AbstractBinder,我关注了this answer

0 个答案:

没有答案
相关问题