簡體   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