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

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


    網站導航:
     
    主站蜘蛛池模板: 亚洲无码高清在线观看| 亚洲色无码一区二区三区| 一级人做人爰a全过程免费视频| 美女被免费网站视频在线| 国产大片91精品免费观看不卡| 亚洲精品国自产拍在线观看| 久久精品国产亚洲av麻豆图片| 亚欧国产一级在线免费| 国产免费资源高清小视频在线观看| 亚洲成a人片在线观看播放| 免费无码又爽又刺激一高潮| 亚洲欧洲自拍拍偷精品 美利坚| 亚洲成av人片在线天堂无| 国产人在线成免费视频| 久久久久se色偷偷亚洲精品av| 日韩免费高清大片在线| 久久精品国产亚洲AV麻豆王友容| 国产中文字幕在线免费观看| 亚洲人成人无码网www电影首页| 成人毛片100免费观看| 亚洲级αV无码毛片久久精品| 中国性猛交xxxxx免费看| 国产亚洲免费的视频看| 久久久久久一品道精品免费看| 亚洲黄色三级视频| 国产在线观看免费观看不卡| 亚洲av无一区二区三区| 亚洲第一视频在线观看免费| 999zyz**站免费毛片| 一区二区三区亚洲| 国产精品免费看久久久久| 永久免费观看黄网站| 久久精品7亚洲午夜a| 免费视频爱爱太爽了| 精品在线观看免费| 久久亚洲熟女cc98cm| 国产一区二区免费在线| 无码日韩精品一区二区三区免费 | 亚洲日韩一中文字暮| 亚洲精品天堂成人片?V在线播放| 国产成人免费ā片在线观看老同学 |