java學(xué)習(xí)

          java學(xué)習(xí)

           

          spring中aop 的配置實(shí)現(xiàn)

          四種通知的執(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="http://www.springframework.org/schema/beans"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:p="http://www.springframework.org/schema/p"
                   xmlns:aop="http://www.springframework.org/schema/aop"
                   xmlns:tx="http://www.springframework.org/schema/tx"
                 xmlns:context="http://www.springframework.org/schema/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

          ">
          <bean id="myXmlAspect" class="com.yjw.aspect.MyXmlAspect"/>
          <!-- 配置事務(wù)切面-->
          <aop:config>
           <aop:aspect ref="myXmlAspect">
            <aop:pointcut expression="execution(* com.yjw.dao..*.*(..))" id="pointcut"/>
            <aop:before method="beforeAdvice" pointcut-ref="pointcut"/>
            <aop:after-returning method="afterReturningAdvice" returning="value" pointcut-ref="pointcut"/>
            <aop:after-throwing method="exceptionAdvice" pointcut-ref="pointcut" throwing="ex"/>
            <aop:after method="afterAdvice" pointcut-ref="pointcut"/>
           </aop:aspect>
          </aop:config>
          </beans>

           

          package com.yjw.aspect;

           

           

          public class MyXmlAspect {


           
           //前置通知

           public void  beforeAdvice(){
            System.out.println("前置通知");
           }
           //異常通知,接收異常信息

           public  void  exceptionAdvice(Exception  ex){
            System.out.println("異常出現(xiàn)"+ex.getMessage());
           }
           //后置通知,可以接收方法的返回值
           
           public  void  afterReturningAdvice(Object  value){
            System.out.println("后置通知"+value);
           }
           //最終通知

           public  void  afterAdvice(){
            System.out.println("after");
           }
          }

           

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


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


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

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 富川| 江安县| 阳高县| 饶平县| 永善县| 商城县| 阿拉善右旗| 灵台县| 兴文县| 陕西省| 洛南县| 广宗县| 彩票| 西城区| 台湾省| 如东县| 英超| 平塘县| 曲阳县| 乐昌市| 双鸭山市| 稷山县| 宁南县| 濉溪县| 岳普湖县| 蒲江县| 诸暨市| 长汀县| 堆龙德庆县| 韶山市| 卢龙县| 林州市| 寻甸| 金门县| 邻水| 潮州市| 潞西市| 外汇| 佛学| 阿城市| 青州市|