使用Guice框架编写基于Annotation的Method Interceptor时无法注入java对象

Sat*_*Sat 7 java aop guice microservices

我的应用程序结构就像

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

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SampleAnnotation {
}
Run Code Online (Sandbox Code Playgroud)

然后创建了一个Sample Interceptor:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

}

我使用上述注释的类是

// 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;
      }
}
Run Code Online (Sandbox Code Playgroud)

我有一个RestModule使用我绑定SampleClassForInterceptor 如下

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

现在我有一个绑定类的bootsrap类 RestModule

public class Bootstrap extends ServerBootstrap {
   binder.install(new RestModule());
}
Run Code Online (Sandbox Code Playgroud)

用法:-

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

    public void someMethod() {
        sampleClassForInterceptor.sampleMethod();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

Sat*_*Sat 0

当我requestInjection在 中使用时SampleModule,然后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);
     }
}
Run Code Online (Sandbox Code Playgroud)