千山鳥飛絕 萬徑人蹤滅
          勤練內(nèi)功,不斷實踐招數(shù)。爭取早日成為武林高手
          Spring提供了兩種切面聲明方式,實際工作中我們可以選用其中一種:
                基于XML配置方式聲明切面。
                基于注解方式聲明切面。
          要進行AOP編程,首先我們要在spring的配置文件中引入aop命名空間:
          <beans xmlns="http://www.springframework.org/schema/beans"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:aop="http://www.springframework.org/schema/aop"
                 xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
          </beans>


          基于注解方式聲明切面

          @Aspect
          public class LogPrint {
           @Pointcut("execution(* cn.itcast.service..*.*(..))")
           private void anyMethod() {}//聲明一個切入點 
           @Before("anyMethod() && args(userName)")//定義前置通知
           public void doAccessCheck(String userName) {
           } 
           @AfterReturning(pointcut="anyMethod()",returning="revalue")//定義后置通知
           public void doReturnCheck(String revalue) {
           }
           @AfterThrowing(pointcut="anyMethod()", throwing="ex")//定義例外通知
              public void doExceptionAction(Exception ex) {
           }
           @After("anyMethod()")//定義最終通知
           public void doReleaseAction() {
           }
           @Around("anyMethod()")//環(huán)繞通知
           public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
            return pjp.proceed();
           }
          }



          基于基于XML配置方式聲明切面


          public class LogPrint {
           public void doAccessCheck() {}定義前置通知
           public void doReturnCheck() {}定義后置通知
              public void doExceptionAction() {}定義例外通知
           public void doReleaseAction() {}定義最終通知
           public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
            return pjp.proceed();環(huán)繞通知
           }
          }


          <bean id="orderservice" class="cn.itcast.service.OrderServiceBean"/>
          <bean id="log" class="cn.itcast.service.LogPrint"/>
          <aop:config>
            <aop:aspect id="myaop" ref="log">
             <aop:pointcut id="mycut" expression="execution(* cn.itcast.service..*.*(..))"/>
             <aop:before pointcut-ref="mycut" method="doAccessCheck"/>
             <aop:after-returning pointcut-ref="mycut" method="doReturnCheck "/>
             <aop:after-throwing pointcut-ref="mycut" method="doExceptionAction"/>
             <aop:after pointcut-ref="mycut" method=“doReleaseAction"/>
             <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
            </aop:aspect>
          </aop:config>
           

          posted on 2009-09-02 12:14 笑口常開、財源滾滾來! 閱讀(2181) 評論(0)  編輯  收藏 所屬分類: spring學習
           
          主站蜘蛛池模板: 米泉市| 平定县| 米脂县| 青浦区| 长汀县| 葫芦岛市| 安康市| 阳曲县| 开江县| 宜城市| 长白| 突泉县| 黄山市| 赤壁市| 忻州市| 喀喇沁旗| 苏州市| 金坛市| 霍山县| 东乡县| 佳木斯市| 徐州市| 繁峙县| 环江| 峨眉山市| 三穗县| 汝南县| 凌云县| 怀远县| 江孜县| 宜丰县| 镇安县| 阿拉善右旗| 乐安县| 西宁市| 浮山县| 阳春市| 永昌县| 古交市| 鄂托克前旗| 喀喇沁旗|