java學習

          java學習

           

          spring中aop 的配置實現

          四種通知的執行地方:前置通知
          try{
          業務代碼
          后置通知
          } 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"/>
          <!-- 配置事務切面-->
          <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("異常出現"+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)  編輯  收藏


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 忻城县| 阳朔县| 丰城市| 漯河市| 观塘区| 定陶县| 伊川县| 东辽县| 贵州省| 余姚市| 蒙山县| 澄迈县| 姜堰市| 阿拉善左旗| 通榆县| 哈巴河县| 九龙城区| 麦盖提县| 饶河县| 天台县| 抚州市| 林甸县| 武邑县| 江永县| 西城区| 清水河县| 东港市| 二手房| 肥乡县| 临汾市| 奉节县| 平泉县| 永康市| 安多县| 和龙市| 香格里拉县| 亚东县| 利辛县| 林西县| 维西| 余江县|