【发布时间】:2019-04-29 02:59:33
【问题描述】:
程序的完整结构
注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface UserAnnotation {
}
然后创建了一个拦截器:
public class UserInterceptor implements MethodInterceptor {
private static final Logger logger = LoggerFactory.getLogger(UserInterceptor.class);
@Inject
UserService userService; // this is not working
public Object invoke(MethodInvocation invocation) throws Throwable {
logger.info("UserInterceptor : Interceptor Invoked");
Object result = invocation.proceed();
Observable<List<User>> observable = (Observable<List<Sample>>) result;
observable.flatMap(Observable::from).subscribe(object -> {
User user = (User)object
SampleSender sender = new SampleSender();
sender.setBoolean(user.isBoolean());
logger.info("Pushing Data into Sender");
userService.insert(String.join("_", "key", "value"), sender);
}
return result;
}
}
然后我创建了一个 GuiceModule 如下:-
public class UserModule extends AbstractModule {
@Override
protected void configure() {
SampleInterceptor interceptor = new SampleInterceptor()
requestInjection(interceptor);
bindInterceptor(Matchers.any(), Matchers.annotatedWith(SampleAnnotation.class), interceptor);
}
}
我使用上述注释的类是
// This class also have so many method and this was already declared and using in another services, I created a sample class here
class UserClassForInterceptor {
@Inject
AnotherClass anotherClass;
// this userMethod() 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
@UserAnnotation
public Observable<List<Sample>> userMethod() {
logger.info("This is printing only once");
return anotherClass.getUser().flatMap(user ->{
logger.info("This is also printing twice");
// this logger printed twise means, this code snippet is getting executed twise
});
}
}
public class AnotherClass{
public Observable<User> getUser(){
Observable<Sample> observableSample = methodReturnsObservableSample();
logger.info("Getting this logger only once");
return observableSample.map(response-> {
logger.info("This logger is printing twice");
//here have code to return observable of User
});
}
}
如果我删除可观察对象内的注释记录器,则只会打印一次,但是当我使用注释时,这些记录器会被打印两次。我不知道为什么会这样。
我有一个RestModule,我使用它来绑定UserClassForInterceptor,如下所示
public final class RestModule extends JerseyServletModule {
// other classes binding
bind(UserClassForInterceptor.class).in(Scopes.SINGLETON);
// other classes binding
install(new SampleModule());
}
现在我有一个引导类,我在其中绑定RestModule
public class Bootstrap extends ServerBootstrap {
binder.install(new RestModule());
}
用法:-
@Path("service/sample")
public class SampleRS {
@Inject
UserClassForInterceptor userClassForInterceptor;
public void someMethod() {
userClassForInterceptor.sampleMethod();
}
}
【问题讨论】:
-
您的
UserAnnotation是如何实现的?那将是寻找双重调用的第一个地方。此外,您在getTestObject()中缺少return关键字。 -
我已经更新了代码和
UserAnnocation实现,其用法等与我在链接中分享的SampleAnnotation相同 -
在
getTestObject()中,您创建一个以getUser().flatMap...开头的观察者链。但是,它没有任何反应,因为没有订阅。您向我们展示的代码不会表现出您描述的行为。 -
用我的示例应用程序结构更新问题
-
我再说一遍,在
getTestObject()方法中,getUser().flatMapUser( user -> { ... });这个语句是没有作用的,因为没有订阅。该语句内的日志记录语句无法执行。这意味着您没有向我们展示展示您描述的行为的代码。