AspectJ學習筆記

  1. 介紹

    1. AspectJ是一個基于Java語言的AOP框架
    2. Spring2.0以后新增了對AspectJ切點表達支持
    3. @AspectJ是AspectJ1.5新增功能,通過JDK5注解技術,允許Bean類中定義切面,新版本Spring框架,建議使用AspectJ方式來開發AOP
    4. 主要用途:自定義開發
  2. 切入點表達式

    1. execution() 用于描述方法

      <!--創建目標類-->
      <bean id="userService" class="com.adolph.AOP.jdk.UserServiceImpl"></bean><!--創建切面類-->
      <bean id="myAspect" class="com.adolph.AOP.jdk.MyAspect"></bean><!--aop編程使用<aop:config>進行配置proxy-target-class="true":使用cglib代理<aop:pointcut>切入點,從目標對象獲得具體方法<aop:advisor> 特殊的切面,只有一個通知和一個切入點advice-ref:通知引用pointcut-ref:切入點引用切入點表達式:execution(* com.adolph.AOP.jdk.*.*(..))選擇方法 返回值任意  包 類名任意 方法名任意 參數任意
      -->
      <aop:config proxy-target-class="true"><aop:pointcut id="myPointCut" expression="execution(* com.adolph.AOP.jdk.*.*(..))"/><aop:advisor advice-ref="myAspect" pointcut-ref="myPointCut"/>
      </aop:config>
      復制代碼
      1. 語法:execution(修飾符 返回值 包.類.方法(參數) throws 異常)

        1. 修飾符:一般省略
        2. 返回值:*(任意)
          1. 省略
            1. con.adolph.cn 固定包
            2. com.adolph. adolph包下面子包任意*
            3. com.adolph.. adolph包下面的所有子包(含自己)
          1. 省略
          2. UserServiceImpl 指定類
          3. *Impl 以Impl結尾
          4. User 以User開頭*
          5. *. 任意
        3. 方法名
          1. 不能省略
          2. addUser 固定方法
          3. add 以add開頭*
          4. *Do 以Do結尾
          5. *. 任意
        4. 參數
          1. () 無參
          2. (int) 一個整型
          3. (int,int) 兩個
          4. (..) 參數任意
        5. throws
          1. 可省略,一般不寫
      2. 綜合

           `execution(* com.adolph.AOP.jdk.*.*(..))`
        復制代碼
  3. AspectJ通知類型

    1. aop聯盟定義通知類型,具有特性接口,必須實現,從而確定方法名稱
    2. aspectj 通知類型,只定義類型名稱。以及方法格式
    3. 個數:6種,知道5種,掌握一種
      1. before:前置通知
      2. afterReturning:后置通知(應用:常規數據處理)
      3. around:環繞通知(應用:十分強大,可以做任何事情)
        1. 方法執行前后分別執行、可以阻止方法的執行
        2. 必須手動執行目標方法
      4. afterThrowing:拋出異常通知(應用:包裝異常信息)
      5. after:最終通知(應用:清理現場)
  4. 基于XML

    1. 目標類:接口+實現

    2. 切面類:編寫多個通知,采用aspectJ通知名稱任意(方法名任意)

    3. aop編程:將通知應用到目標類

    4. 測試

    5. 目標類

      public class UserServiceImpl {public void addUser() {System.out.println("addUser");}public void updateUser() {System.out.println("updateUser");}public void deleteUser() {System.out.println("deleteUser");}
      }
      復制代碼
    6. 切面類

      /*** 切面類,含有多個通知*/
      public class MyAspect {public void myBefore(JoinPoint joinPoint) {System.out.println("前置通知" + joinPoint.getSignature().getName()); //獲得目標方法名}public void myAfterReturning(JoinPoint joinPoint, Object ret) {System.out.println("后置通知" + joinPoint.getSignature().getName() + "____" + ret); //獲得目標方法名}public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{System.out.println("前");//手動執行目標方法Object proceed = joinPoint.proceed();System.out.println("后");return proceed;}public void MyAfterThrowing(JoinPoint joinPoint,Throwable e){System.out.println("拋出異常通知:");}public void after(JoinPoint joinPoint){System.out.println("最終");}
      }
      復制代碼
    7. xml

      <!--創建目標類-->
      <bean id="userService" class="com.adolph.AOP.jdk.UserServiceImpl"></bean><!--創建切面類-->
      <bean id="myAspect" class="com.adolph.AOP.jdk.MyAspect"></bean>
      <!--aop編程
      -->
      <aop:config><!--將切面類 聲明成切面,從而獲得通知(方法),ref:切面類引用--><aop:aspect ref="myAspect"><!--聲明一個切入點,所有的通知都可以使用expression:切入點表達式id:名稱用于其他通知引用--><aop:pointcut id="myPointcut" expression="execution(* com.adolph.AOP.jdk.UserServiceImpl.*(..))"></aop:pointcut><!--前置通知method:通知,及方法名pointcut:切入點表達式,此表達式只能當前通知使用pointcut-ref:切入點引用,可以與其他通知點共享切入點通知方法格式:public void myBefore(JoinPoint joinPoint)參數JoinPoint 用于描述連接點(目標方法),獲得目標方法名稱等--><aop:before method="myBefore" pointcut-ref="myPointcut"></aop:before><!--后置通知,目標方法執行,獲得返回值public void myAfterReturning(JoinPoint joinPoint,Object ret)參數1:連接點描述參數2:類型Object,參數名returning配置的--><aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut" returning="ret"></aop:after-returning><!--環繞通知public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable返回值:Object方法名:任意參數:Proceeding JoinPoint拋出異常--><aop:around method="myAround" pointcut-ref="myPointcut"></aop:around><!--異常通知throwing:通知方法的第二個參數名稱--><aop:after-throwing method="MyAfterThrowing" pointcut-ref="myPointcut" throwing="e"></aop:after-throwing><!--最終通知--><aop:after method="after" pointcut-ref="myPointcut"></aop:after></aop:aspect>
      </aop:config>
      復制代碼
  5. 基于注解

    1. 替換bean

      
      <!--掃描-->
      <context:component-scan base-package="com.adolph.AOP"></context:component-scan>
      復制代碼
      @Component
      public class MyAspect
      復制代碼
    2. 必須進行aspectj自動代理

      <!--確定aop注解生效-->
      <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
      復制代碼
    3. 聲明切面

      @Component
      @Aspect
      public class MyAspect
      復制代碼
    4. 設置前置通知

         @Before("execution(* com.adolph.AOP.jdk.UserServiceImpl.*(..))")public void myBefore(JoinPoint joinPoint) {System.out.println("前置通知" + joinPoint.getSignature().getName()); //獲得目標方法名}
      復制代碼
    5. 替換公共切入點

      @Pointcut("execution(* com.adolph.AOP.jdk.UserServiceImpl.*(..))")
      private void myPointCut(){}
      復制代碼
    6. 設置后置通知

      @AfterReturning(value = "myPointCut()",returning = "ret")
      public void myAfterReturning(JoinPoint joinPoint, Object ret) {System.out.println("后置通知" + joinPoint.getSignature().getName() + "____" + ret); //獲得目標方法名
      }
      復制代碼
    7. 設置環繞通知

      @Around(value = "myPointCut()")
      public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{System.out.println("前");//手動執行目標方法Object proceed = joinPoint.proceed();System.out.println("后");return proceed;
      }
      復制代碼
    8. 設置拋出異常

      @AfterThrowing(value = "myPointCut()",throwing = "e")
      public void MyAfterThrowing(JoinPoint joinPoint,Throwable e){System.out.println("拋出異常通知:");
      }
      復制代碼
    9. 最終通知

      @AfterThrowing(value = "myPointCut()",throwing = "e")
      public void MyAfterThrowing(JoinPoint joinPoint,Throwable e){System.out.println("拋出異常通知:");
      }
      復制代碼

轉載于:https://juejin.im/post/5ca23972e51d453c0f7d8e93

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/279789.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/279789.shtml
英文地址,請注明出處:http://en.pswp.cn/news/279789.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

windows10訪客_如何在Windows 10中創建訪客帳戶

windows10訪客If you find that your guests are asking fairly often to use your computer temporarily to check their email or look something up on the web, you don’t have to let them use your personal account or create a special account for each guest. 如果發…

C#使用 System.Net.Mail發送郵件功能

介紹System.Net.Mail命名空間是在.NET Framework中新增的&#xff0c;該命名空間提供了發送電子郵件的功能。通過對本章的學習&#xff0c;讀者可以輕松地使用.NET Framework提供的類庫來發送電子郵件。System.Net.Mail 命名空間包含用于將電子郵件發送到SMTP服務器的類&#x…

初識smarty

個人體會(不完全正確)&#xff1a;就是smarty就是為了更好的使得php/html結合做出來的一個框架。 , 轉載于:https://www.cnblogs.com/nul1/p/9357694.html

幾個有趣的算法題目

本文首發 http://svtter.cn最接近的數字 題目 一個K位的數N $$ (K\leq2000&#xff0c;N\leq10^{20}) $$ 找出一個比N大且最接近的數&#xff0c;這個數的每位之和與N相同&#xff0c;用代碼實現之。 例如&#xff1a;0050 所求書數字為0104&#xff1b;112 所求數為121&#x…

獲取一篇新聞的全部信息

給定一篇新聞的鏈接newsUrl&#xff0c;獲取該新聞的全部信息 標題、作者、發布單位、審核、來源 發布時間:轉換成datetime類型 點擊&#xff1a; newsUrlnewsId(使用正則表達式re)clickUrl(str.format(newsId))requests.get(clickUrl)newClick(用字符串處理&#xff0c;或正則…

上twitter_如何在Twitter上更改您的顯示名稱

上twitterUnlike Facebook, Twitter has never insisted people user their real names. In fact, there’s a long tradition of people changing their names to a joke or pun because it’s Christmas or Halloween, or just for no reason at all. 與Facebook不同&#xf…

技術走向管理一些思考(1)-性格特質和自我管理

技術走向管理一些思考-文件夾 1&#xff0c;管理需具備的性格特質 贊賞他人&#xff1a;以一種不以自我為中心的合作的方式和他人相處&#xff0c;能平靜和客觀地接受不同的人。放下自己的性格、喜好&#xff0c;去贊賞不同類型的人。不是通過個人友誼或者熟悉程度。而是通過某…

網橋

配置實現網橋 網橋&#xff1a;即橋接 把一套機器上的若干個網絡接口 “連接” 起來&#xff0c;其結果是&#xff0c;其中一個網口收到的報文會被復制給其他網口并發送出去。以使得網口之間的報文能夠互相轉發。網橋就是這樣一個設備&#xff0c;它有若干個網口&#xff0c;并…

Newtonsoft.Json Deserialize Type 或者 同類型 變量 反序列化

Newtonsoft.Json 經常再用 這樣的需求 還是很少用 場景 方法一&#xff1a;根據 Type 反序列化 int demo 0; string jsongString JsonConvert.SerializeObject(demo); int jsonDemo JsonConvert.DeserializeObject(jsongString, demo.GetType()); 方法二 根據 同類型變量 序…

raspberry pi_在月光下將Raspberry Pi變成蒸汽機

raspberry piValve’s Steam Machines aim to bring your Steam game library right into your living room (but at a rather steep premium). Today we’ll show you how to bring your Steam library (plus all your other computer games) to your living room for a fract…

文檔測試【轉載】

原文來自&#xff1a;51Testing軟件測試網采編 作者&#xff1a; 仙靈測試(sinablog) 原文鏈接&#xff1a;http://www.51testing.com/html/61/n-237961.html 1、文檔的種類 ● 聯機幫助文檔或用戶手冊 這是人們最容易想到的文檔。用戶手冊是隨軟件發布而印制的小冊子…

NOI2019省選模擬賽 第三場

傳送門 明明沒參加過卻因為點進去結果狂掉\(rating\)…… \(A\) 集合 如果我們記 \[f_k\sum_{i1}^nT^i{n-i\choose k}\] 那么答案顯然就是\(f_{k-1}\) 然后就可以開始推倒了 \[ \begin{aligned} f_k &\sum_{i1}^nT^i{n-i\choose k}\\ &\sum_{i1}^nT^i{n-i-1\choose k}\…

MySql數據庫出現 1396錯誤

1、安裝MySql數據庫后。創建新的用戶。有可能會出現 1396這個錯誤&#xff0c; 2、解決的辦法如下&#xff1a;假裝有你需要創建的這個用戶、先刪了。再創建。 3、這樣就可以解決用戶創建不成功的問題了。 轉載于:https://www.cnblogs.com/chifa/p/9362882.html

如何使用wink框架_如何解決Wink Hub的Z-Wave連接問題

如何使用wink框架Overall, the Wink hub works extremely well…but sometimes the devices you have connected to it can act a little wonky. Here are some things you can do in order to fix any connection issues with all of those Z-Wave sensors and devices connec…

Tomcat服務器啟動錯誤之Offending class: javax/servlet/Servlet.class

引子 最近在基于Wex5項目開發中&#xff0c;遇到使用過程中與Tomcat功能有關的錯誤提示&#xff0c; 如題所示。最終的解決方法就是刪除掉項目上與tomcat沖突的jar包。 org.apache.catalina.loader.WebappClassLoader validateJarFile ??: validateJarFile(/Users/zxzpc/…

面向對象進階(二)----------類的內置方法

一、isinstance(obj,cls)和issubclass(sub,super) 1. isinstance(obj,cls): 檢查是否obj是否是類 cls 的對象 class Player:passp Player()print(isinstance(p, Player))>>> Ture 2. issubclass(sub, super): 檢查sub類是否是 super 類的派生類 class Player:passcla…

BZOJ.3265.志愿者招募加強版(費用流SPFA)

題目鏈接 見上題。 每類志愿者可能是若干段&#xff0c;不滿足那個...全幺模矩陣(全單位模矩陣)的條件&#xff0c;所以線性規劃可能存在非整數解。 于是就可以用費用流水過去順便拿個rank2 233. //20704kb 300ms #include <queue> #include <cstdio> #include &…

谷歌相冊_Google相冊中的新存檔功能是什么?

谷歌相冊If you’re a Google Photos user, you’ve may have seen a new feature called “Archive” show up in the app’s sidebar. if not, don’t stress—it’s just now rolling out and not everyone has it yet. Since it’s new, here’s a quick look at what it i…

CenterOS 7安裝Nginx

1.wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm下載對應當前系統版本的nginx包(package) 2.rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm建立nginx的yum倉庫 3.yum install nginx 下載并安裝nginx systemctl s…

Java的組合排列問題

從4個人中選2個人參加活動&#xff0c;一共有6種選法。 從n個人中選m個人參加活動&#xff0c;一共有多少種選法&#xff1f;C(m/n)C((m-1)/(n-1))C(m/(n-1))數學算法 public class Main {public static void main(String[] args) {System.out.println("請輸入總人數:&quo…