国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

面向方面編程AOP和JBoss(二)

2019-11-18 13:04:52
字體:
來源:轉載
供稿:網友

  訪問Metadata
  為了用元數據,它在運行時間必須是可達的。類的元數據是通過Invocation對象可達的。為了在我們的例子使用它,TracingInterceptor必須要修改一點點。
  
  public class TracingInterceptor implements Interceptor
  {
    public String getName() { return TracingInterceptor; }
    public InvocationResponse invoke(Invocation invocation)
      throws Throwable
    {
     String filter = (String)invocation.getMetaData(tracing, filter);
     if (filter != null && filter.equals(true))
    return invocation.invokeNext();
  
     String message = null;
  
     if (invocation.getType() == InvocationType.METHOD)
     {
       Method method = MethodInvocation.getMethod(invocation);
       message    = method: + method.getName();
     }
     else if (invocation.getType() == InvocationType.CONSTRUCTOR)
     {
       Constructor c = ConstructorInvocation.getConstructor(invocation);
       message    = constructor: + c.toString();
     }
     else
     {
       // Do nothing for fields. Just too verbose.
       return invocation.invokeNext();
     }
  
     System.out.PRintln(Entering + message);
  
     // Continue on. Invoke the real method or constructor.
     InvocationResponse rsp = invocation.invokeNext();
     System.out.println(Leaving + message);
     return rsp;
    }
  }
  
  運行例子2
  [code]POJO類將擴展一點,增加get()和set()方法。
  public class POJO
  {
    public POJO() {}
    public void helloWorld() { System.out.println(Hello World!); }
  
    private int counter = 0;
  
    public int getCounter() { return counter; }
    public void setCounter(int val) { counter = val; }
    public static void main(String[] args)
    {
     POJO pojo = new POJO();
     pojo.helloWorld();
     pojo.setCounter(32);
     System.out.println(counter is: + pojo.getCounter());
    }
  }
  TracingInterceptor將攔截對main(),POJO()和helloWorld()調用。輸出應該看起來如下:
  Entering constructor: public POJO()
  Leaving constructor: public POJO()
  Entering method: helloWorld
  Hello World!
  Leaving method: helloWorld
  [/code]
  
  你能夠在這里下載JBoss AOP和離子代碼。編譯和執行:
  $ cd oreilly-aop/example2
  $ eXPort CLASSPATH=.;jboss-common.jar;jboss-aop.jar;javassist.jar
  $ javac *.java
  $ java -Djava.system.class.loader=org.jboss.aop.standalone.SystemClassLoader POJO
  
  例子3.使用導言
  假如我們能夠為特定的實例關閉和打開,那將很酷。JBoss AOP有一個API,他綁定元數據到一個對象實例,但是讓我們偽裝一個實際的跟蹤API是一個更好的方案。在這例子中,我們通過用一個導言,將改變POJO類的本身的定義。我們將強制POJO類去實現一個跟蹤借口和提供混合類,這個混合類處理新的跟蹤API。這將是跟蹤借口:
  
  public interface Tracing
  {
    public void enableTracing();
    public void disableTracing();
  }
  
  定義一個混合的類
  Tracing接口將在混合類中實現。當一個POJO是實例時,一個混合對象混合類將綁定到POJO類。下面是實現:
  
  import org.jboss.aop.Advised;
  
  public class TracingMixin implements Tracing
  {
    Advised advised;
  
    Public TracingMixin(Object obj)
    {
     this.advised = (Advised)obj;
    }
  
    public void enableTracing()
    {
     advised._getInstanceAdvisor().getMetaData().addMetaData(
    "tracing", "filter", true);
    }
  
    public void disableTracing()
    {
     advised._getInstanceAdvisor().getMetaData().addMetaData(
    "tracing", "filter", false);
    }
  }
  
  enableTracing()方法綁定filter屬性到對象實例。在disableTracing()方法作同樣的事,但是制定filter屬性為false。這兩個方法是元數據能夠怎么樣用于超過一個類級別。元數據也能夠實例級的應用。元數據應用在實例級別。
  
  綁定一個導言
  好了,所以我們定義跟蹤接口,并且實現這個混合類。下一步是應用導言到POJO類。像攔截器,我們必須在xml中定義一個ponitcut。讓我們看一下這項什么。
  
  <?xml version="1.0" encoding="UTF-8">
  <aop>
    <introduction-pointcut class="POJO">
     <mixin>
       <interfaces>Tracing</interfaces>
       <class>TracingMixin</class>
       <construction>new TracingMixin(this)</construction>
     </mixin>
    </introduction-pointcut>
  </aop>
  
  上面的pointcuts將強制POJO類實現Tracing接口。現在,當一個POJO實例被初始化,一個TracingMixin也將被實例化。TracingMixin被初始化的途徑被定義在<contstruction>標簽中。你能夠把想要的任一行Java代碼放入在<contstruction>標簽中。
  
  運行例子3
  POJO類為了顯示TracingAPI怎么被訪問,它已經被擴展了一點。TracingInterceptor仍然和例子2一樣。
  
  [code]public class POJO
  {
    public POJO() {}
    public void helloWorld() { System.out.println(Hello World!); }
  
    public static void main(String[] args)
    {
     POJO pojo   = new POJO();
     Tracing trace = (Tracing)this;
     pojo.helloWorld();
  
     System.out.println("Turn off tracing.");
  
     trace.disableTracing();
     pojo.helloWorld();
  
     System.out.println("Turn on tracing.");
  
     trace.enableTracing();
     pojo.helloWorld();
    }
  }
  [/code]
  注重我們轉換POJO到Tracing接口。輸出應該看起來這樣:
  
  Entering constructor: POJO()
  Leaving constructor: POJO()
  Entering method: helloWorld
  Hello World!
  Leaving method: helloWorld
  Turn off tracing.
  Entering method: disableTracing
  Leaving method: disableTracing
  Hello World!
  Turn on tracing.
  Entering method: helloWorld
  Hello World!
  Leaving method: helloWorld
  
  注重被增加到TracingInterceptor 中的interceptor-pointcut也應用到那些通過Tracing 導言導入的方法中。
  為了編譯和運行這個例子:
  
  $ cd oreilly-aop/example3
  $ export CLASSPATH=.;jboss-common.jar;jboss-aop.jar;javassist.jar
  $ javac *.java
  $ java -Djava.system.class.loader=org.jboss.aop.standalone.SystemClassLoader POJO
  
  結論
  面向方面編程對于軟件開發是一個強有力的新工具。為了使你的軟件開發過程更加動態和流暢,用JBoss4.0,你能夠實現你自己的攔截器,元數據和導言。更具體的文檔參見我們的站點www.jboss.org。那會有一些驚異等著你,象我們已經在我們新的框架上實現了一套服務。擁有它并恰當的使用。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永泰县| 河北省| 惠东县| 平顶山市| 襄城县| 东光县| 汶川县| 黔东| 台中市| 克拉玛依市| 林芝县| 会同县| 通许县| 康保县| 方正县| 庆城县| 金华市| 琼结县| 会昌县| 玉门市| 志丹县| 铜川市| 石泉县| 林芝县| 华蓥市| 建水县| 酒泉市| 五指山市| 彰化县| 合作市| 惠州市| 杨浦区| 泊头市| 公安县| 沾化县| 黔西| 玉龙| 绥德县| 麻城市| 镇远县| 上林县|