java學(xué)習(xí)

          java學(xué)習(xí)

           

          spring中aop的annotation的寫法

          四種通知的執(zhí)行地方:前置通知
          try{
          業(yè)務(wù)代碼
          后置通知
          } catch{
          異常通知
          } finally{
          最終通知
          }
          1.需要的jar包:aspectjrt.jar,aspectjweaver.jar,cglib-nodep-2.1.3.jar
          2.在配置文件中加入

          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="       xmlns:xsi="        xmlns:p="         xmlns:aop="         xmlns:tx="       xmlns:context="       xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

          ">
          <!-- 讓spring支持annotation的aop -->
          <aop:aspectj-autoproxy/>
          <!-- 把切面類交給spring -->
          <bean id="myAspece" class="com.yjw.aspect.MyAspect"/>

          </beans>

          package com.yjw.aspect;


          import org.aspectj.lang.annotation.After;
          import org.aspectj.lang.annotation.AfterReturning;
          import org.aspectj.lang.annotation.AfterThrowing;
          import org.aspectj.lang.annotation.Aspect;
          import org.aspectj.lang.annotation.Before;
          import org.aspectj.lang.annotation.Pointcut;
          //定義切面類,存放通知
          @Aspect
          public class MyAspect {

           //切入點(diǎn)
           /**第一個(gè)星號代表返回任何類型的數(shù)據(jù),bean后的兩個(gè)點(diǎn)代表本包及其子包,
            * 第二個(gè)星號代表中間的所有的類;
            * 第三個(gè)星號代表中間的所有的方法;
            * (..)小括號代表參數(shù)列表,兩個(gè)點(diǎn)代表參數(shù)類型不限
            */
           
           @Pointcut("execution(* com.yjw.dao..*.*(..))")
           public void  pointCut(){}
           //前置通知
           @Before(" pointCut()")
           public void  beforeAdvice(){
            System.out.println("前置通知");
           }
           //異常通知,接收異常信息
           @AfterThrowing( pointcut=" pointCut()",throwing="ex")
           public  void  exceptionAdvice(Exception  ex){
            System.out.println("異常出現(xiàn)"+ex.getMessage());
           }
           //后置通知,可以接收方法的返回值
           @AfterReturning( pointcut=" pointCut()",returning="value")
           public  void  afterReturningAdvice(Object  value){
            System.out.println("后置通知"+value);
           }
           //最終通知
           @After(" pointCut()")
           public  void  afterAdvice(){
            System.out.println("zuizhong");
           }

          }


          posted on 2013-04-29 11:35 楊軍威 閱讀(243) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 阳西县| 荣昌县| 保山市| 周宁县| 罗田县| 华坪县| 富阳市| 蕉岭县| 吉隆县| 库尔勒市| 辽阳县| 徐闻县| 康定县| 蕉岭县| 广汉市| 枣阳市| 同江市| 天津市| 墨脱县| 四会市| 呼和浩特市| 陆河县| 永吉县| 安多县| 和龙市| 海原县| 炎陵县| 吴桥县| 辰溪县| 津南区| 舟山市| 宁明县| 潼关县| 西乌珠穆沁旗| 通渭县| 潍坊市| 郁南县| 临武县| 四会市| 佛冈县| 镇平县|