@Around 環繞通知的使用。


 1 package com.bjpowernode.ba03;
 2 
 3 import org.aspectj.lang.JoinPoint;
 4 import org.aspectj.lang.ProceedingJoinPoint;
 5 import org.aspectj.lang.annotation.AfterReturning;
 6 import org.aspectj.lang.annotation.Around;
 7 import org.aspectj.lang.annotation.Aspect;
 8 
 9 import java.util.Date;
10 
11 @Aspect
12 public class MyAspect {
13     /**
14      * 環繞通知方法的定義格式
15      * 1. public
16      * 2. 必須有一個返回值,推薦使用Object
17      * 3. 方法名稱自定義
18      * 4. 方法有參數,固定的參數ProceedingJoinPoint
19      */
20     /**
21      * @Around:環繞通知
22      * 屬性:value 切入點表達式
23      * 位置 在方法的定義前后執行
24      *
25      * 特點:
26      * 1. 他是功能最強的通知
27      * 2. 在目標方法的前后都能增強功能
28      * 3. 控制目標方法是否被調用執行
29      * 4. 修改原來的目標方法的執行結果,影響最后的調用結果
30      * 環繞通知等同於JDK動態代理的InvocationHandler接口
31      *
32      * 參數:ProceedingJoinPoint 就等同於Method,作用:執行目標方法的
33      * 返回值:就是目標方法的執行結果,可以被修改。
34      *
35      * 環繞通知:經常做事務,在目標方法之前開啟事務,執行目標方法,目標方法執行完成后結束事務
36      */
37     @Around(value="execution(* *First(String,Integer))" )
38     public Object myAround(ProceedingJoinPoint pjp) throws Throwable {
39         //獲取目標方法的參數
40         String name="";
41         Object args[] = pjp.getArgs();
42         if(args!=null && args.length>1){
43             Object arg=args[0];
44             name = (String) arg;
45         }
46         //實現環繞通知
47         Object result = null;
48         System.out.println("環繞通知:在目標方法之前輸出時間"+new Date());
49         //1. 目標方法調用
50         Object[] objs = pjp.getArgs();
51         if("虎虎虎1".equals(name)){//可以控制目標方法的執行
52             result = pjp.proceed();//Method.invoke;Object result = doFirst(); 執行目標方法
53         }
54         //2. 在目標方法之后加入功能
55         System.out.println("環繞通知:在目標方法之后,提交事務");
56         //返回目標方法執行的結果,可以修改目標方法的執行結果,影響目標方法的最終返回值
57         return  result+"ddd";
58 
59     }
60 }
 1 package com.bjpowernode;
 2 
 3 import com.bjpowernode.ba03.SomeService;
 4 import org.junit.Test;
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 
 8 public class MyTest01 {
 9     @Test
10     public void test01(){
11         ApplicationContext ac=  new ClassPathXmlApplicationContext("ApplicationContext.xml");
12         SomeService someService = (SomeService) ac.getBean("myService");
13         someService.doSome("張三",33);
14         someService.doOther("李四",21);
15         String  res = someService.doFirst("虎虎虎" ,12);
16         System.out.println(res);
17     }
18 }
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:aop="http://www.springframework.org/schema/aop"
4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
5     <bean id="myService" class="com.bjpowernode.ba03.SomeServiceImpl"/>
6     <bean id="myAspect" class="com.bjpowernode.ba03.MyAspect"/>
7     <aop:aspectj-autoproxy/>
8 </beans>

 

執行了doSome方法!
執行了doOther方法!
環繞通知:在目標方法之前輸出時間Thu Mar 04 22:35:43 CST 2021
環繞通知:在目標方法之后,提交事務
nullddd


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM