繁体   English   中英

我的 Google Guice 方法拦截器不执行但为什么?

[英]my Google Guice method interceptor doesn't execute but Why?

所以我正在测试一个简单的 Google Guice 拦截器 -

我的注解——

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

}

我的拦截器

public class AppOpsOperationDecorator implements MethodInterceptor {

    private ServiceCallStack callStack = null ;

    @Inject
    public void setServiceCallStack(ServiceCallStack stack ){
        callStack = stack ;
    }

    @Override
    public Object invoke(MethodInvocation arg0) throws Throwable {

        // Retrieve the call stack 
        // exclude service population if caller service is the same service 
        // else push the current service onto top of stack 

        System.out.println("Intercepting method  -- ::  " + arg0.getMethod().getName());
        System.out.println("On object             - ::  " + arg0.getThis().getClass().getName());
        System.out.println("On accessible object  - ::  " + arg0.getStaticPart().getClass().getName());
        return invocation.proceed();
    }

}

现在我的服务接口和方法

public interface MockCalledService extends AppOpsService {

@AppOpsOperation
public String methodOneCalled(String some);

@AppOpsOperation
public String methodTwoCalled(String some);
}

public class MockCalledServiceImpl extends BaseAppOpsService implements MockCalledService {

@Override
@AppOpsOperation
public String methodOneCalled(String some) {
    System.out.println("MockCalledServiceImpl.methodOneCalled()");
    return this.getClass().getCanonicalName() + "methodOneCalled";
}

@Override
public String methodTwoCalled(String some) {
    System.out.println("MockCalledServiceImpl.methodTwoCalled()");
    return this.getClass().getCanonicalName() + "methodTwoCalled";
}

}

还有我的 Guice 测试模块

public class MockTestGuiceModule extends AbstractModule {

@Override
protected void configure() {

    bind(ServiceCallStack.class).toInstance(new ServiceCallStack());

    AppOpsOperationDecorator decorator = new AppOpsOperationDecorator() ;

    requestInjection(decorator);

    bindInterceptor(Matchers.any(), Matchers.annotatedWith(AppOpsOperation.class), 
             decorator);

    bind(MockCalledService.class).toInstance(new MockCalledServiceImpl());
}

}

当我运行下面的测试时,这个拦截器不会执行 -

public class AppOpsOperationDecoratorTest {

private Injector injector = null ;

@Before
public void init(){
    injector =  Guice.createInjector(new MockTestGuiceModule());
}

@Test
public void testDecoratorInvocation() {

    MockCalledService called = injector.getInstance(MockCalledService.class);

    called.methodOneCalled("Test String");

}

}

你能强调一下我做错了什么吗?

找到真正的原因后,我来回答。 它是如此简单以至于它真的很棘手。

方法拦截仅在您将接口与类绑定而不是此实现的实例时才有效。

所以代替bind(MockCalledService.class).toInstance(new MockCalledServiceImpl());

我们应该写bind(MockCalledService.class).to(MockCalledServiceImpl.class);

似乎实例没有被代理:(

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM