麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

面向方面編程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。那會有一些驚異等著你,象我們已經在我們新的框架上實現了一套服務。擁有它并恰當的使用。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 在线高清中文字幕 | free国产hd老熟bbw | 欧美一级片一区 | 成人午夜精品久久久久久久3d | 免费看一级视频 | 毛片免费看电影 | 在线成人一区二区 | 精品一区二区三区在线观看视频 | 91短视频版高清在线观看免费 | 国产亚洲精品久久午夜玫瑰园 | 91在线色视频 | 1区2区3区在线观看 欧美特黄a | 久久99国产精品视频 | 欧美一级黄色影院 | 中文字幕在线一 | 在线观看国产一区二区三区 | 国产精品成人久久久久a级 av电影在线免费 | 日本精品免费观看 | 91精品观看91久久久久久国产 | 国产精品视频亚洲 | 成人免费福利网站 | 久久影院午夜 | 亚洲国产美女视频 | 久久影院一区二区三区 | 26uuu成人人网图片 | 国产在线精品一区二区夜色 | 国产男女爽爽爽爽爽免费视频 | 911精品影院在线观看 | 91九色视频观看 | 国产人成精品综合欧美成人 | 草久影视 | 91短视频在线免费观看 | 一级免费在线 | 在线看一级片 | 亚洲乱妇19p | 视频国产一区二区 | 91美女视频在线观看 | 午夜91视频 | 久久免费视频7 | 在线小视频国产 | 国产毛片在线高清视频 |