<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

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

    切面類,去除掉和AOP有關(guān)的注解,變得非常簡潔
     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("環(huán)繞開始切入點:execute==>" + pjp.getTarget() + "==>"
    20                 + pjp.getSignature().getName() + " method");
    21         pjp.proceed();
    22         Logger.info("環(huán)繞結(jié)束切入點: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實現(xiàn) -->
    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             <!-- 環(huán)繞 -->
    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 
    結(jié)果和使用annotation無異:
    前置切入點:execute==>org.duyt.dao.impl.UserDao@af08a49==>add method
    環(huán)繞開始切入點:execute==>org.duyt.dao.impl.UserDao@af08a49==>add method
    用戶增加方法
    后置切入點:execute==>org.duyt.dao.impl.UserDao@af08a49==>add method
    環(huán)繞結(jié)束切入點:execute==>org.duyt.dao.impl.UserDao@af08a49==>add method

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

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導(dǎo)航

    統(tǒng)計

    公告

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

    常用鏈接

    留言簿

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 成年男女免费视频网站| 亚洲免费观看在线视频| 亚洲国产av无码精品| 亚洲成av人无码亚洲成av人| 在线a毛片免费视频观看| 亚洲免费观看在线视频| 久草视频在线免费| 亚洲一级毛片免观看| 国产一精品一AV一免费孕妇 | 亚洲欧洲国产综合| 曰批全过程免费视频观看免费软件| 在线中文高清资源免费观看| 亚洲AV无码国产一区二区三区| 久久成人免费大片| 亚洲精品自拍视频| 成人性生交大片免费看午夜a| 日韩亚洲综合精品国产| 亚洲av区一区二区三| 日批视频网址免费观看| 亚洲bt加勒比一区二区| 99久久免费国产精精品| 亚洲91av视频| 大学生高清一级毛片免费| 一级特黄色毛片免费看| 四虎国产精品免费视| 亚洲www在线观看| 国产成人无码区免费A∨视频网站| 亚洲国产精品日韩在线| 日韩免费a级在线观看| 亚洲视频在线观看2018| jizz日本免费| 久久久亚洲裙底偷窥综合| 免费观看成人毛片a片2008| 猫咪免费人成网站在线观看入口| 亚洲免费人成在线视频观看| 亚洲人成在线免费观看| 国产亚洲Av综合人人澡精品| 亚洲AV午夜成人片| 日本免费一区二区三区最新| a级毛片在线免费看| 亚洲日韩AV一区二区三区四区|