我的应用程序结构是这样的
我创建了如下注释:-
@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());
}
我在其中使用上述注释的类是
// 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
,我使用它来绑定(bind)SampleClassForInterceptor
,如下所示
public final class RestModule extends JerseyServletModule {
// other classes binding
bind(SampleClassForInterceptor.class).in(Scopes.SINGLETON);
// other classes binding
install(new SampleModule());
}
现在我有一个 bootsrap 类,我在其中绑定(bind) 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()
之前开始执行,然后在执行 sampleMethod()
之后再次返回到拦截器,现在这里我有一个代码片段,它将插入结果(我们从 sampleMethod()
获得)。这是我得到 NullPointerException
的地方,我检查了代码,发现 SampleService
对象没有被注入(inject),它的值为 null
注意:我正在使用具有 RESTFUL 服务概念的微服务
最佳答案
当我在 SampleModule
中使用 requestInjection
时,SampleService
被注入(inject)到拦截器中,即我修改了 SampleModule
代码如下
public class SampleModule extends AbstractModule {
@Override
protected void configure() {
SampleInterceptor interceptor = new SampleInterceptor();
requestInjection(interceptor);
bindInterceptor(Matchers.any(), Matchers.annotatedWith(SampleAnnotation.class), interceptor);
}
}
关于java - 使用 Guice 框架编写基于注释的方法拦截器时无法注入(inject) java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53149896/