繁体   English   中英

如何在Java中拦截特定接口?

[英]How do intercept specific interface in java?

MyInterceptor ,哪个接口IfclocalIfcRemote调用了MyEjb doStuff方法? 可能知道通过哪个“渠道”调用了您的bean? 我需要知道哪个依赖项注入调用了该方法。

@Local
public interface IfcLocal {
void doStuff(String s);
}

@Remote
public interface IfcRemote {
  void doStuff(String s);
}

@Stateless
@Interceptors({ MyInterceptor.class })
public class MyEjb implements IfcLocal, IfcRemote {
  @Override
  public void doStuff(String s) {
     System.out.println(s);
  }
}

public class MyManagedBean {
  @EJB private ifcLocal ifcLocal;
  @EJB private IfcRemote ifcRemote;

  public void go() {
    ifcLocal.doStuff("xxx");
    ifcRemote.doStuff("xxx");
  }
}

public class MyInterceptor {
  @AroundInvoke
  public Object intercept(InvocationContext inv) throws Exception {
    // ??? who invoked ???
    System.out.prinln(inv.getTarget().getClass()); // print MyEjb 
  }
}

注入@Resource private SessionContext sessionContext; MyInterceptor 之后: Class<?> interfaceReference = sessionContext.getInvokedBusinessInterface()

暂无
暂无

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

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