千山鳥飛絕 萬徑人蹤滅
          勤練內功,不斷實踐招數。爭取早日成為武林高手
          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 笑口常開、財源滾滾來! 閱讀(2187) 評論(0)  編輯  收藏 所屬分類: spring學習
           
          主站蜘蛛池模板: 玛曲县| 深州市| 玛多县| 大荔县| 手机| 廉江市| 买车| 和田县| 陆川县| 黄龙县| 伽师县| 河津市| 长海县| 古田县| 陆川县| 安义县| 闻喜县| 云林县| 广元市| 普安县| 万盛区| 荆州市| 北川| 四会市| 红桥区| 英德市| 离岛区| 南岸区| 准格尔旗| 锡林浩特市| 昭平县| 南投市| 隆化县| 宁国市| 龙州县| 丁青县| 桃园市| 社会| 万荣县| 客服| 正镶白旗|