【发布时间】:2015-06-19 09:50:35
【问题描述】:
我试图找到 Guice MethodInterceptor 应该返回的答案。返回 methodInvocation.proceed(); 有什么区别?并返回 null;
这是我的情况:在某些情况下,用户有权调用某些方法,而在某些情况下则没有。我想使用 guice aop 来实现这种情况。
如果我不想调用方法,我应该返回什么?返回 null 和任何其他对象有什么区别。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AOPEstablisher{
}
class GuiceModule extends AbstractModule{
@Override
protected void configure() {
GuiceExampleAop guiceExampleAop = new GuiceExampleAop ();
requestInjection(guiceExampleAop);
bindInterceptor(Matchers.any(), Matchers.annotatedWith(AOPEstablisher.class), guiceExampleAop );
}
}
class CommandExecutor{
@AOPEstablisher
public void executeInSomeCases(){
//I want to execute this command in some cases
}
}
这里是拦截器类:
import org.aopalliance.intercept.MethodInterceptor;
public class GuiceExampleAop implements MethodInterceptor {
@Inject
User user;
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
if(user.hasRigths(){
return methodInvocation.proceed();
else{
return null?
return methodInvocation?
//what should be returned here?
// what is difference between returning methodInvocation and null
}
}
}
感谢您的帮助。
【问题讨论】:
-
顺便说一句:将用户注入方法拦截器时会遇到问题,因为您必须在 bind() 期间自己实例化拦截器 ...