<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Java世界

    學習筆記

    常用鏈接

    統計

    積分與排名

    天籟村

    新華網

    雅虎

    最新評論

    Method interception

    Overview

    JBoss AOP allows you to insert behavior between the caller of a method and the actual method being called. If you look at Driver.java you will see that it is invoking a number of methods declared in POJO.java. JBoss AOP allows you to intercept a method call and transparently insert behavior when the method is invoked.

    What is an Interceptor?

    Behavior that you want to insert when a method is executed must be encapsulated in a implementation of the org.jboss.aop.advice.Interceptor interface. (For those of you familiar with AOP terms, an Interceptor in JBoss is an aspect with only one advice.)
    package org.jboss.aop.advice;
    
    import org.jboss.aop.joinpoint.Invocation;
    
    public interface Interceptor
    {
       public String getName();
       public Object invoke(Invocation invocation) throws Throwable;
    }
    

    When an AOP'ed method is called, JBoss will break up the method into its parts: a java.lang.reflect.Method object and an

    Object[]
    array representing the arguments of the method. These parts are encapsulated in an org.jboss.aop.joinpoint.Invocation object. SimpleInterceptor.java is a simple implementation of an interceptor.

    How do I apply an Interceptor to a method execution?

    To bind an interceptor to a method execution, you must create an XML file. Open up jboss-aop.xml and take a look. Let's first start by applying SimpleInterceptor.java to the POJO.noop() method.
    <aop>
    ...
       <bind pointcut="execution(public void POJO->noop())">
           <interceptor class="SimpleInterceptor"/>
       </bind>
    ...
    </aop>
    
    To apply the interceptor you must create a binding and a pointcut that specifies where in your Java code you want the interceptor applied. execution(method expression) defines whenever the method noop() is executed. A method expression requires a return type followed by a class expression followed by '->' followed by the method name followed by a list of parameters. You can optionally provide method attributes like 'public', 'static', etc. if so desired.

    Method expressions

    You do not have to specify the entire signature of the method and can use wildcards anywhere you want. The next binding defined in jboss-aop.xml looks like this.
       <bind pointcut="execution(* POJO->*(int))">
           <interceptor class="MethodInterceptor"/>
       </bind>
    
    This binding says, whenever any POJO method that has one parameter that is an int is executed, invoke the MethodInterceptor. The next bindings shows another example.
       <bind pointcut="execution(static * POJO->*(..))">
           <interceptor class="MethodInterceptor"/>
       </bind>
    
    This binding says, whenever any static POJO method is executed invoke the MethodInterceptor

       <bind pointcut="execution(* POJO$Bar->*(..))">
           <interceptor class="MethodInterceptor"/>
       </bind>
    
    The above binding shows how to specify an inner class.

    Decomposing the Interceptor class

    When an intercepted method is executed, the AOP framework will call each bound interceptor in a chain within the same call stack. The Invocation object drives the chain. Interceptors call invocation.invokeNext() to proceed with the method invocation. After the chain is exhausted, Java reflection is called to execute the actual method. Because this is one call stack, you can place try/catch/finally blocks around invocation.invokeNext() to catch any exceptions thrown by the executed method if you so desired.

    Each type of intercepted execution (method, constructor, field, etc.) has a specific class that extends the base class org.jboss.aop.joinpoint.Invocation. If you open up MethodInterceptor.java, you will see that you can typecast the Invocation parameter into a org.jboss.aop.joinpoint.MethodInvocation object. The MethodInvocation class allows you to obtain additional information about the particular method call like the java.lang.reflect.Method object representing the call, the arguments, and even the targetObject of the interception.

    Configure with XML

    If you want to do some XML configuration of the interceptor instance, you can have it implement org.jboss.util.xml.XmlLoadable.

    public interface XmlLoadable
    {
       public void importXml(Element element);
    }
    

    Running

    To compile and run:
      $ ant
    
    It will javac the files and then run the AOPC precompiler to manipulate the bytecode, then finally run the example. The output should read as follows:
    run:
         [java] --- pojo.noop(); ---
         [java] <<< Entering SimpleInterceptor
         [java] noop()
         [java] >>> Leaving SimpleInterceptor
         [java] --- pojo.test1(String param); ---
         [java] test1(String param): hello world
         [java] --- pojo.test1(int param); ---
         [java] <<< Entering MethodInterceptor for: public void POJO.test2(int)
         [java] test2(int param): 55
         [java] >>> Leaving MethodInterceptor
         [java] --- POJO.test2(); ---
         [java] <<< Entering MethodInterceptor for: public static void POJO.test2()
         [java] static method
         [java] >>> Leaving MethodInterceptor
    

    posted on 2008-03-20 09:10 Rabbit 閱讀(370) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲乱妇熟女爽到高潮的片| 亚洲一本综合久久| 亚洲乱亚洲乱妇24p| 99久久国产热无码精品免费 | 国产成人久久精品亚洲小说| 国产成人午夜精品免费视频| 亚洲国产成人久久77| 中文字幕无码播放免费| 亚洲一区二区三区高清视频| 99久久99这里只有免费费精品| 亚洲一区二区三区久久| 久久精品女人天堂AV免费观看| 亚洲欧洲AV无码专区| 免费看一级做a爰片久久| 青草青草视频2免费观看| 国产国拍亚洲精品福利 | 亚洲AV无码国产精品永久一区| 日本不卡在线观看免费v| 男男黄GAY片免费网站WWW| 亚洲国产精品成人一区| 伊人免费在线观看| 久久丫精品国产亚洲av| 噼里啪啦电影在线观看免费高清 | 亚洲精品无码av人在线观看| 无码国产精品一区二区免费式芒果| 亚洲国产成人资源在线软件| 免费看大黄高清网站视频在线| 无码人妻一区二区三区免费视频 | 亚洲精品无码日韩国产不卡av| 亚洲av成人一区二区三区在线观看| 国产裸体美女永久免费无遮挡| 亚洲AV日韩精品久久久久| 国产桃色在线成免费视频| 国产亚洲精品免费| 亚洲婷婷五月综合狠狠爱| 91热成人精品国产免费| 在线观看亚洲专区| 亚洲av日韩av高潮潮喷无码| 特级做A爰片毛片免费69| 99精品全国免费观看视频..| 亚洲国产日产无码精品|