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

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

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

    posts - 21,  comments - 6,  trackbacks - 0

    Spring AOP Framework

    ?

    Here's my little exploration to Spring's AOP framework - a little interceptor which just logs which class is called and which method is called, plus logging the method invocation time; however I hope this can help others to understand Spring's AOP and help them to write interceptors of their own.

    -cptechno


    An interceptor used in Spring need to implement the org.aopalliance.intercept.MethodInterceptor interface, which requires implementing this method:



    public? Object?invoke(MethodInvocation?methodInvocation)? throws? Throwable;



    And next, comes that little interceptor...



    import? org.aopalliance.intercept.MethodInterceptor;
    import? org.aopalliance.intercept.MethodInvocation;
    import? org.apache.commons.logging.Log;
    import? org.apache.commons.logging.LogFactory;

    public?class? MyInterceptor? implements? MethodInterceptor
    {
    ?? private?final? Log?logger?=?LogFactory.getLog(getClass());

    ?? public? Object?invoke(MethodInvocation?methodInvocation)? throws? Throwable
    ?? {
    ???? logger.info( "Beginning?method:?"? +?methodInvocation.getMethod().getDeclaringClass()?+? "::"? +?methodInvocation.getMethod().getName());
    ???? long? startTime?=?System.currentTimeMillis();
    ???? try
    ???? {
    ?????? Object?retVal?=?methodInvocation.proceed();
    ?????? return? retVal;
    ???? }
    ???? finally
    ???? {
    ?????? logger.info( "Ending?method:?"?? +?methodInvocation.getMethod().getDeclaringClass()?+? "::"? +?methodInvocation.getMethod().getName());
    ?????? logger.info( "Method?invocation?time:?"? +?(System.currentTimeMillis()?-?startTime)?+? "?msecs." );
    ???? }
    ?? }

    }



    You can do anything as you like; but pay attention to these two lines:



    Object?retVal?=?methodInvocation.proceed();
    return? retVal;



    The execution sequence is as follows:

    1. Any statements placed before Object retVal = methodInvocation.proceed();
    2. Object retVal = methodInvocation.proceed(); , which gives control to the next interceptor in the interceptor stack, or the underlying method.
    3. Any statements placed before return retVal;
    4. return retVal; , which returns control to the interceptor above it, or exit the whole interceptor stack.

    Next, to use the interceptor we wrote, we need to turn our business object as an AOP target, like this:



    <bean?id= "SearchBookBeanTarget"? class = "library.SearchBookBeanImpl"? init-method= "init"? />



    As shown, we just need to change the bean's id.

    Next we need to hang the interceptor on to Spring's ApplicationContext.



    <bean?id= "myInterceptor"? class = "library.MyInterceptor"? />



    And the last step, we declare our business object actually in the ApplicationContext, via its interface we defined, via Spring's ProxyFactoryBean.



    <bean?id= "SearchBookBean"? class = "org.springframework.aop.framework.ProxyFactoryBean" >
    ?? <property?name= "proxyInterfaces" ><value>library.SearchBookBean</value></property>
    ???? <property?name= "interceptorNames" >
    ?????? <list>
    ???????? <value>myInterceptor</value>
    ???????? <value>SearchBookBeanTarget</value>
    ?????? </list>
    ???? </property>
    ?? </bean>

    • proxyInterfaces: the actual business interface of our business object.
    • interceptorNames: the execution sequence of the interceptors, with the business object's target as the end of the list. Remember to put the business object's target on the list, otherwise your business object will not work; on the other hand you'll receive an exception telling you that all interceptors had been invoked.

    On the application code that will access the business object, no changes are necessary.



    Then at your logging target (console, file, etc...) you can see the following output similar to this (time and level info trimmed here):



    Beginning?method:? interface? library.SearchBookBean::searchBook
    ....
    (log?messages?about?library.SearchBookBean.searchBook()....)
    ....
    Ending?method:? interface? library.SearchBookBean::searchBook
    Method?invocation?time:? 10? msecs.

    posted on 2006-09-27 21:07 Warren.Wu 閱讀(277) 評論(0)  編輯  收藏

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 精品久久久久久久久免费影院| 亚洲国产成人精品无码区花野真一 | 久久亚洲免费视频| 四虎永久免费地址在线观看| 亚洲日韩久久综合中文字幕| eeuss在线兵区免费观看| 妞干网免费观看视频| 亚洲中文字幕久久精品无码A| 一级毛片免费毛片毛片| 久久国产精品萌白酱免费| 精品国产亚洲一区二区三区| 麻豆狠色伊人亚洲综合网站 | 久久精品亚洲综合| 国产99在线|亚洲| 9久9久女女免费精品视频在线观看| 亚洲人成综合在线播放| 永久免费无码日韩视频| 久久99亚洲综合精品首页| h片在线观看免费| 亚洲天堂中文字幕| caoporm超免费公开视频| 国产亚洲一区二区三区在线| 午夜无码A级毛片免费视频 | 波多野结衣一区二区免费视频| 西西人体44rt高清亚洲| 91精品国产免费久久久久久青草| 亚洲精品亚洲人成在线观看下载 | 亚洲AV无码国产精品色| 国产一区二区免费| 亚洲精品国产成人中文| 性感美女视频免费网站午夜| 一级毛片人与动免费观看| 亚洲精选在线观看| 成人毛片免费观看视频大全| 亚洲狠狠狠一区二区三区| 免费黄色网址入口| 国产久爱免费精品视频| 亚洲一区二区视频在线观看| 免费A级毛片无码专区| 亚洲色欲色欲www在线丝| 理论片在线观看免费|