千山鳥飛絕 萬徑人蹤滅
          勤練內功,不斷實踐招數。爭取早日成為武林高手
          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()")//環繞通知
           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();環繞通知
           }
          }


          <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學習
           
          主站蜘蛛池模板: 扶沟县| 德化县| 达孜县| 阿鲁科尔沁旗| 女性| 交口县| 朝阳县| 武功县| 阳朔县| 深泽县| 甘南县| 磐安县| 安化县| 绥德县| 甘德县| 江永县| 新平| 沁源县| 鄂伦春自治旗| 察隅县| 安康市| 鄄城县| 石楼县| 革吉县| 宿州市| 车致| 米林县| 常山县| 桃园县| 保山市| 武汉市| 顺义区| 成都市| 涿鹿县| 大石桥市| 衡山县| 云龙县| 格尔木市| 历史| 海门市| 正阳县|