使用 Guice 框架编写基于注释的方法拦截器时无法注入 java 对象

Not able to inject java object while writing Annotation based Method Interceptor using Guice framework

我的申请结构是这样的

我创建了一个注释如下:-

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SampleAnnotation {
}

然后创建了一个示例拦截器:

public class SampleInterceptor implements MethodInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(SampleInterceptor.class);

    @Inject
    SampleService sampleService; // this is not working

    public Object invoke(MethodInvocation invocation) throws Throwable {
        logger.info("SampleInterceptor : Interceptor Invoked");
        Object result = invocation.proceed();
        Observable<List<Sample>> observable = (Observable<List<Sample>>) result;
        SampleSender sender = null;
        List<Sample> sampleList = observable.toBlocking().first();

        for(Sample sample : sampleList ) {
            sender = new SampleSender();
            sender.setBoolean(sample.isBoolean());
            logger.info("Pushing Data into Sender");
            sampleService.insert(String.join("_", "key", "value"), sender); // here getting NullPointerException because sampleService is null
        }
        return result;
    }
}

然后我创建了一个 GuiceModule 如下:-

public class SampleModule extends AbstractModule {
    @Override
    protected void configure() {
        bindInterceptor(Matchers.any(), Matchers.annotatedWith(SampleAnnotation.class), new SampleInterceptor());
}

}

Class 其中我使用上面的注解是

// This class also have so many method and this was already declared and using in another services, I created a sample class here
class SampleClassForInterceptor {

      // this sampleMethod() is not a new method, its already created, 
      // now I am adding annotation to it, because after finishing this functionality, 
      // I want something should be done, so created annotation and added here
      @SampleAnnotation
      public Observable<List<Sample>> sampleMethod() {
            Sample sample = new Device();
            sample.setName("*** 7777");
            sample.setBoolean(true);
            List<Sample> list = new ArrayList<>();
            list.add(sample);
            Observable<List<Device>> observable = Observable.just(list);
            return observable;
      }
}

我有一个 RestModule,我正在使用它来绑定 SampleClassForInterceptor,如下所示

public final class RestModule extends JerseyServletModule {
    // other classes binding
    bind(SampleClassForInterceptor.class).in(Scopes.SINGLETON);
    // other classes binding
    install(new SampleModule());
}

现在我有一个 bootsrap class,我在其中绑定 RestModule

public class Bootstrap extends ServerBootstrap {
   binder.install(new RestModule());
}

用法:-

@Path("service/sample")
public class SampleRS {
    @Inject
    SampleClassForInterceptor sampleClassForInterceptor;

    public void someMethod() {
        sampleClassForInterceptor.sampleMethod();
    }
}

我的拦截器功能在执行 SampleClassForInterceptor sampleMethod() class 之前开始执行,然后在执行 sampleMethod() 后再次返回拦截器,现在我有一个将插入结果的代码片段(我们从 sampleMethod() 获得)。这是我得到 NullPointerException 的地方,我检查了代码,发现 SampleService 对象没有被注入,它的值是 null

注意:我正在使用具有 RESTFUL 服务概念的微服务

当我在 SampleModule 中使用 requestInjection 时,SampleService 被注入到拦截器中,即我修改了 SampleModule 代码如下

public class SampleModule extends AbstractModule {
     @Override
     protected void configure() {
         SampleInterceptor interceptor = new SampleInterceptor();
         requestInjection(interceptor);
         bindInterceptor(Matchers.any(), Matchers.annotatedWith(SampleAnnotation.class), interceptor);
     }
}