<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 閱讀(271) 評論(0)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 亚洲精品天堂成人片AV在线播放| 亚洲狠狠综合久久| 亚洲AV日韩综合一区| 曰批全过程免费视频在线观看| 久久精品亚洲一区二区| 久久伊人免费视频| 亚洲美女激情视频| www视频在线观看免费| 亚洲一级毛片视频| 成年女人毛片免费观看97| 亚洲情A成黄在线观看动漫软件 | 三上悠亚亚洲一区高清| www一区二区www免费| 亚洲精品无码成人AAA片| 成人性生交大片免费看好| 91亚洲国产成人精品下载| 成人黄色免费网址| 亚洲国产精品无码久久九九大片| 国产一级一片免费播放| www成人免费视频| 亚洲av日韩av激情亚洲| 免费H网站在线观看的| 亚洲国产成人综合精品| 久久亚洲国产成人精品无码区| 免费日本一区二区| 亚洲中文字幕无码av| 亚洲成a人片在线观看老师| 国精产品一区一区三区免费视频| 亚洲系列国产精品制服丝袜第| 成年女人免费视频播放77777| 国产亚洲蜜芽精品久久| 亚洲国产精品无码中文字| 无码人妻一区二区三区免费手机 | 色屁屁www影院免费观看视频| 亚洲综合色婷婷七月丁香| 69国产精品视频免费| 国产偷国产偷亚洲高清人| 亚洲爆乳精品无码一区二区三区| 青青在线久青草免费观看| 成人精品综合免费视频| 亚洲精品视频免费在线观看|