溫故知新:spring_07基于XML配置AOP

          相比較于使用annotation去配置,使用XML去配置AOP反而簡單些,無需在切面類中為每個通知方法定義切入點表達式,切面類簡潔且不受代碼入侵,非常的靈活。

          切面類,去除掉和AOP有關的注解,變得非常簡潔
           1 package org.duyt.autoProxy;
           2 
           3 import org.aspectj.lang.JoinPoint;
           4 import org.aspectj.lang.ProceedingJoinPoint;
           5 import org.duyt.util.Logger;
           6 import org.springframework.stereotype.Component;
           7 
           8 @Component("loggerAspect")
           9 public class LoggerAspect {
          10     public void loggerBefore(JoinPoint jp) {
          11         Logger.info("前置切入點:execute==>" + jp.getTarget() + "==>"
          12                 + jp.getSignature().getName() + " method");
          13     }
          14     public void loggerAfter(JoinPoint jp) {
          15         Logger.info("后置切入點:execute==>" + jp.getTarget() + "==>"
          16                 + jp.getSignature().getName() + " method");
          17     }
          18     public void loggerAround(ProceedingJoinPoint pjp) throws Throwable {
          19         Logger.info("環繞開始切入點:execute==>" + pjp.getTarget() + "==>"
          20                 + pjp.getSignature().getName() + " method");
          21         pjp.proceed();
          22         Logger.info("環繞結束切入點:execute==>" + pjp.getTarget() + "==>"
          23                 + pjp.getSignature().getName() + " method");
          24     }
          25 }
          26 
          beans.xml變更為以下的配置
           1 <?xml version="1.0" encoding="UTF-8"?>
           2 <beans xmlns="http://www.springframework.org/schema/beans"
           3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
           4     xmlns:aop="http://www.springframework.org/schema/aop"
           5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
           6                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           7                         http://www.springframework.org/schema/context
           8                         http://www.springframework.org/schema/context/spring-context-3.0.xsd
           9                         http://www.springframework.org/schema/aop
          10                         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
          11 
          12     <!-- 開啟注解 -->
          13     <context:annotation-config />
          14     <!-- 指定哪些需要加入掃描 -->
          15     <context:component-scan base-package="org.duyt.*" />
          16 
          17     <!-- 開啟AOP切面自動代理 -->
          18     <!-- 無須再開啟 -->
          19     <!-- <aop:aspectj-autoproxy /> -->
          20 
          21     <!-- 使用xml配置AOP實現 -->
          22     <aop:config>
          23         <!-- 定義切面類并引用 -->
          24         <aop:aspect id="loggerAspect" ref="loggerAspect">
          25             <!-- 定義切入點 -->
          26             <aop:pointcut id="loggerPointcut"
          27                 expression="execution (* org.duyt.dao.*.add*(..))||
          28                             execution (* org.duyt.dao.*.delete*(..))||
          29                             execution (* org.duyt.dao.*.update*(..))" />
          30             <!-- 定義通知,前置 -->
          31             <aop:before method="loggerBefore" pointcut-ref="loggerPointcut"/>
          32             <!-- 后置 -->
          33             <aop:after method="loggerAfter" pointcut-ref="loggerPointcut"/>
          34             <!-- 環繞 -->
          35             <aop:around method="loggerAround" pointcut-ref="loggerPointcut"/>
          36         </aop:aspect>
          37     </aop:config>
          38 
          39 </beans>
          測試類
           1 package org.duyt.test;
           2 
           3 import org.duyt.action.UserAction;
           4 import org.junit.Test;
           5 import org.springframework.beans.factory.BeanFactory;
           6 import org.springframework.context.support.ClassPathXmlApplicationContext;
           7 
           8 public class IocTest {
           9 
          10     private BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
          11     
          12     @Test
          13     public void test(){
          14         //基于XML的AOP
          15         UserAction ua = (UserAction) factory.getBean("userAction");
          16         ua.addUser();
          17     }
          18     
          19 }
          20 
          結果和使用annotation無異:
          前置切入點:execute==>org.duyt.dao.impl.UserDao@af08a49==>add method
          環繞開始切入點:execute==>org.duyt.dao.impl.UserDao@af08a49==>add method
          用戶增加方法
          后置切入點:execute==>org.duyt.dao.impl.UserDao@af08a49==>add method
          環繞結束切入點:execute==>org.duyt.dao.impl.UserDao@af08a49==>add method

          posted on 2014-11-05 22:45 都較瘦 閱讀(336) 評論(0)  編輯  收藏 所屬分類: containerFramework

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          公告

          博客定位:囿于目前的水平,博客定位在記錄自己的學習心得和隨手的練習

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 满洲里市| 二连浩特市| 崇礼县| 肃南| 南和县| 东港市| 稻城县| 手游| 九江市| 唐河县| 星座| 张北县| 长武县| 邵阳市| 河津市| 怀宁县| 鄂尔多斯市| 久治县| 永善县| 社旗县| 铜山县| 民丰县| 尤溪县| 吉林省| 南充市| 土默特左旗| 乌审旗| 蒲江县| 玉环县| 德化县| 蒙山县| 温宿县| 哈尔滨市| 阿瓦提县| 即墨市| 双桥区| 喜德县| 龙山县| 宝鸡市| 白水县| 毕节市|