本文目錄一覽:
- 1、AOP環繞通知中proceed方法可以有一個Object參數不知有何用處
- 2、Spring aop 關於around環繞通知幾點疑惑,該如何處理怎麼解決
- 3、java spring 環繞通知 ProceedingJoinPoint 執行proceed方法的作用是什麼
- 4、雲南北大青鳥java培訓告訴你動態SpringAOP的是如何實現的?
AOP環繞通知中proceed方法可以有一個Object參數不知有何用處
Java代碼
// 環繞通知
@Around(“anyMethod() args(id)”)
public Object Around(ProceedingJoinPoint pjp, Integer id) throws Throwable {
Object result = null;
if (id == 4) {
System.out.println(id);
result = pjp.proceed();
} else {
result = “我被改變了”;
}
return result;
}
這是切面中環繞通知的一個方法。其中一個pjp.proceed()方法個人理解為是一個對業務方法的模擬,可是在這個方法前後插入想做的事情。
Spring aop 關於around環繞通知幾點疑惑,該如何處理怎麼解決
您好,這樣的:這個還真沒有 好像 , 你自己寫一個吧! 就不判斷methodName直接判斷Method 這個對象。
切面的優先級
為項目增加一個新的切面類,負責驗證功能,則需要指定切面執行的順序。即切面的優先級。具體方法是給切面類增加@Order註解,並指定具體的數字,值越小優先級越高
1 package com.yl.spring.aop;
2
3 import java.util.Arrays;
4
5 import org.aspectj.lang.JoinPoint;
6 import org.aspectj.lang.annotation.Aspect;
7 import org.aspectj.lang.annotation.Before;
8 import org.springframework.core.annotation.Order;
9 import org.springframework.stereotype.Component;
10
11 /**
12 * 可以使用@Order註解指定切面的優先級,值越小優先級越高
13 * @author yul
14 *
15 */
16 @Order(2)
17 @Component
18 @Aspect
19 public class ValidationAspect {
20
21 @Before(“execution(public int com.yl.spring.aop.ArithmeticCalculator.*(..))”)
22 public void vlidateArgs(JoinPoint joinPoint) {
23 System.out.println(“validate: ” + Arrays.asList(joinPoint.getArgs()));
24 }
25 }
切點表達式的重用:
在LoggingAspect類中,切點的表達式可以先定義,在使用。
1 package com.yl.spring.aop;
2
3 import java.util.Arrays;
4
5 import org.aspectj.lang.JoinPoint;
6 import org.aspectj.lang.ProceedingJoinPoint;
7 import org.aspectj.lang.annotation.After;
8 import org.aspectj.lang.annotation.AfterReturning;
9 import org.aspectj.lang.annotation.AfterThrowing;
10 import org.aspectj.lang.annotation.Around;
11 import org.aspectj.lang.annotation.Aspect;
12 import org.aspectj.lang.annotation.Before;
13 import org.aspectj.lang.annotation.Pointcut;
14 import org.springframework.core.annotation.Order;
15 import org.springframework.stereotype.Component;
16 @Order(1)
17 @Component
18 @Aspect
19 public class LoggingAspect {
20
21 /**
22 * 定義一個方法,用於聲明切入點表達式。一般的,該方法中再不需要添加其他的代碼
23 * 使用@Pointcut 來聲明切入點表達式
24 * 後面的其他通知直接使用方法名直接引用方法名即可
25 */
26 @Pointcut(“execution(public int com.yl.spring.aop.ArithmeticCalculator.*(..))”)
27 public void declareJoinPointExpression() {
28
29 }
30
31 /**
32 * 在com.yl.spring.aop.ArithmeticCalculator接口的每一個實現類的每一個方法開始之前執行一段代碼
33 */
34 @Before(“declareJoinPointExpression()”)
35 public void beforeMethod(JoinPoint joinPoint) {
36 String methodName = joinPoint.getSignature().getName();
37 Object[] args = joinPoint.getArgs();
38 System.out.println(“The method ” + methodName + ” begins with ” + Arrays.asList(args));
39 }
40
41 /**
42 * 在com.yl.spring.aop.ArithmeticCalculator接口的每一個實現類的每一個方法執行之後執行一段代碼
43 * 無論該方法是否出現異常
44 */
45 @After(“declareJoinPointExpression()”)
46 public void afterMethod(JoinPoint joinPoint) {
47 String methodName = joinPoint.getSignature().getName();
48 Object[] args = joinPoint.getArgs();
49 System.out.println(“The method ” + methodName + ” ends with ” + Arrays.asList(args));
50 }
51
52 /**
53 * 方法正常結束後執行的代碼
54 * 返回通知是可以訪問到方法的返回值的
55 */
56 @AfterReturning(value=”declareJoinPointExpression()”, returning=”result”)
57 public void afterReturning(JoinPoint joinPoint, Object result) {
58 String methodName = joinPoint.getSignature().getName();
59 System.out.println(“The method ” + methodName + ” return with ” + result);
60 }
61
62 /**
63 * 在方法出現異常時會執行的代碼
64 * 可以訪問到異常對象,可以指定在出現特定異常時在執行通知代碼
65 */
66 @AfterThrowing(value=”declareJoinPointExpression()”, throwing=”ex”)
67 public void afterThrowing(JoinPoint joinPoint, Exception ex) {
68 String methodName = joinPoint.getSignature().getName();
69 System.out.println(“The method ” + methodName + ” occurs exception: ” + ex);
70 }
71
72 /**
73 * 環繞通知需要攜帶ProceedingJoinPoint類型的參數
74 * 環繞通知類似於動態代理的全過程:ProceedingJoinPoint類型的參數可以決定是否執行目標方法。
75 * 而且環繞通知必須有返回值,返回值即為目標方法的返回值
76 */
77 @Around(“declareJoinPointExpression()”)
78 public Object aroundMethod(ProceedingJoinPoint pjd) {
79 Object result = null;
80 String methodName = pjd.getSignature().getName();
81 //執行目標方法
82 try {
83 //前置通知
84 System.out.println(“The method ” + methodName + ” begins with ” + Arrays.asList(pjd.getArgs()));
85 result = pjd.proceed();
86 //返回通知
87 System.out.println(“The method ” + methodName + ” ends with ” + Arrays.asList(pjd.getArgs()));
88 } catch (Throwable e) {
89 //異常通知
90 System.out.println(“The method ” + methodName + ” occurs expection : ” + e);
91 throw new RuntimeException(e);
92 }
93 //後置通知
94 System.out.println(“The method ” + methodName + ” ends”);
95 return result;
96 }
97
98 }
當處於不同的類,甚至不同的包時,可以使用包名.類名.方法名
具體代碼如下:
1 package com.yl.spring.aop;
2
3 import java.util.Arrays;
4
5 import org.aspectj.lang.JoinPoint;
6 import org.aspectj.lang.annotation.Aspect;
7 import org.aspectj.lang.annotation.Before;
8 import org.springframework.core.annotation.Order;
9 import org.springframework.stereotype.Component;
10
11 /**
12 * 可以使用@Order註解指定切面的優先級,值越小優先級越高
13 * @author yul
14 *
15 */
16 @Order(2)
17 @Component
18 @Aspect
19 public class ValidationAspect {
20
21 @Before(“com.yl.spring.aop.LoggingAspect.declareJoinPointExpression()”)
22 public void vlidateArgs(JoinPoint joinPoint) {
23 System.out.println(“validate: ” + Arrays.asList(joinPoint.getArgs()));
24 }
25 }
java spring 環繞通知 ProceedingJoinPoint 執行proceed方法的作用是什麼
環繞通知ProceedingJoinPoint 執行proceed方法的作用是讓目標方法執行,這也是環繞通知和前置、後置通知方法的一個最大區別。
這是Spring框架最基礎的部分,它提供了依賴注入(DependencyInjection)特徵來實現容器對Bean的管理。
這裡最基本的概念是BeanFactory,它是任何Spring應用的核心。BeanFactory是工廠模式的一個實現,它使用IoC將應用配置和依賴說明從實際的應用代碼中分離出來。
擴展資料:
輕量——從大小與開銷兩方面而言Spring都是輕量的。完整的Spring框架可以在一個大小只有1MB多的JAR文件里發佈。
並且Spring所需的處理開銷也是微不足道的。此外,Spring是非侵入式的:典型地,Spring應用中的對象不依賴於Spring的特定類。
控制反轉——Spring通過一種稱作控制反轉(IoC)的技術促進了松耦合。當應用了IoC,一個對象依賴的其它對象會通過被動的方式傳遞進來,而不是這個對象自己創建或者查找依賴對象。
參考資料來源:百度百科-spring框架
雲南北大青鳥java培訓告訴你動態SpringAOP的是如何實現的?
SpringAOP是利用代理模式,在運行時生成一個目標對象的代理,並且使用代理代替目標對象,整個過程對使用者透明,使用者無法像使用目標對象一樣使用代理對象,代理對象類型是目標對象所屬類的子類或者接口實現,麗江IT培訓認為這個子類也是在運行時動態生成,這個生成子類的過程使用操作位元組碼技術,Spring框架中使用兩種位元組碼技術:JDK動態代理和CGLIB,當目標類實現了接口時使用JDK動態代理,否則使用CGLIB代理。
AOP的實現包含下面幾個步驟:
根據配置或註解解析切面。
生成AOP代理對象,給目標對象生成一個代理類以及代理類實例,根據解析出的切面,生成通知鏈設置到代理對象,在代理的回調中會執行通知鏈。
把AOP代理對象註冊到容器中代替目標對象,當使用者向容器請求目標bean時,容器會返回代理對象。
下面對這幾個步驟逐一的分析。
切面解析
在分析切面解析過程之前,首先先了解一下幾個關鍵的接口,看下面的類圖。
PointCut:描述切點,在進行切點匹配時,使用ClassFilter進行類匹配,MethodMatcher進行執行方法匹配。
Advice:通知,AfterAdvice後通知,BeforeAdvice前通知,DynamicIntroductionAdvice引用通知,環繞通知通過Interceptor實現。
Advisor:通知器,也就是切面,PointcutAdvisor切點通知器,IntroductionAdvisor引用通知器。
在創建AOP代理之前需要把相關的切面配置解析成上面類圖中的接口子類的對象,對於ProxyFactoryBean來說,沒有這個過程,因為這種方式下不能使用切點。
切面解析完成之後,把解析出的通知添加通知鏈中,AOP代理對象引用該通知鏈執行切面通知邏輯。對於aop標籤方式和註解方式添加通知鏈這個動作的代碼是類似的,解析切面這個過程有些差異。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/293728.html