@Interceptor 铪铪铪铪铪铪铪

来源:互联网 发布:长兴古银杏王数据 编辑:程序博客网 时间:2024/05/21 07:56

EJB3 為了可以支援 AOP ( swanky : AOP Presentation - http://www.ice.ntnu.edu.tw/~swanky/presentation/PL_AOP.pdf )
所以建立了Interceptors 的觀念.

不過, 雖然是放在 ejb3 api 的製作之中, 但是他的 package 為 javax.interceptor.*, 換句話說, 未來有機會可能會移出 ejb-api .其實, 熟稔 AOP 概念的人 ( AspectJ, AspectWerkz ) 通常會對於 EJB3 目前僅提供 Interceptor 實作的方法, 覺得比較陽春. 但是, 也可以這麼思考, 利用 Interceptor 應該就可以達到一般人的需求 !

在 Interceptor, 只有 InvocationContext Interface, 以及 四個 Annotation Types.
* AroundInvoke
* ExcludeClassInterceptors
* ExcludeDefaultInterceptors
* Interceptors

我們在呼叫任何一個 EJB Bean Class 的時候, 可以利用 @Interceptors 來設定

@Interceptors(MyCallbackHandler.class)
public class MyEJB {
  ....
}


或是利用 deployment descriptor ( ejb-jar.xml ) 來定義 interceptors



 

    ...


 

    

       com.softleader.interceptors.MyCallbackHandler

      

         someInteceptorMethod

      
 

    


 


 

     

         *

         com.softleader.interceptors.MyCallbackHandler

     
 

     

         SlessEJB

         true

     


     

         SlessEJB2

         true

     


 


非常明顯的, 我們可以看到, 我們需要利用 interceptor-binding 將 interceptor-class 指向到所有(*) 或單一(EJBName) 的結合. 以及是否去除 ( exclude-default-interceptors ) 預設的 binding 原則.

一個簡單的 Interceptor 的撰寫方式
import javax.interceptor.InvocationContext;
public class MyCallbackHandler {
    public void someInteceptorMethod(InvocationContext invCtx) {
        System.out.println("Hello Inteceptor in pre-destroy callback methods ");
        try {
            invCtx.proceed();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

當呼叫 InvocationContext.proceed() 之後, 將會進行下一個 interceptor 的作業.

所以我們必須利用 InvocationContext 來取得我們攔截下來取得相關的資訊, 進行相關的處理.

Map getContextData()
Method getMethod()
Object[] getParameters()
Object getTarget()
Object proceed()
void setParameters()

除了控制生命週期的攔截之外, 我們可以利用 ArroundInvoke() 進行相關的攔截

@AroundInvoke
Object aroundInvoke(InvocationContext ctx) throws Exception {
   Common.aroundInvokeCalled(ctx, "testArroundInvoke");
  Object instance = ctx.getTarget();
  if( instance instanceof SomeEJB) {
      ((SomeEJB) instance).aroundInvokeCalled = true;
   }
   return ctx.proceed();
}

這樣就可以建立基本的 AOP 實作了



原创粉丝点击