千山鳥飛絕 萬徑人蹤滅
          勤練內(nèi)功,不斷實(shí)踐招數(shù)。爭(zhēng)取早日成為武林高手
          Spring提供了兩種切面聲明方式,實(shí)際工作中我們可以選用其中一種:
                基于XML配置方式聲明切面。
                基于注解方式聲明切面。
          要進(jìn)行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() {}//聲明一個(gè)切入點(diǎn) 
           @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 笑口常開、財(cái)源滾滾來! 閱讀(2187) 評(píng)論(0)  編輯  收藏 所屬分類: spring學(xué)習(xí)
           
          主站蜘蛛池模板: 乡城县| 百色市| 垣曲县| 石屏县| 织金县| 江城| 天门市| 柞水县| 和平区| 克东县| 吉首市| 南雄市| 文登市| 东海县| 苍南县| 准格尔旗| 东莞市| 灵宝市| 恩平市| 得荣县| 陵川县| 岐山县| 略阳县| 昂仁县| 抚松县| 高台县| 永泰县| 定西市| 禹州市| 剑河县| 东乌珠穆沁旗| 浪卡子县| 定兴县| 龙岩市| 富宁县| 石嘴山市| 炎陵县| 琼海市| 常熟市| 绥德县| 长葛市|