文章目錄
- spring 切面筆記
-
- 切面AOP
- 切面術語
- spring AOP 使用的都是AspectJ(兩種模式注解或配置檔案)
-
- 注解
-
- 注解支援
- 注解聲明切面
- xml配置檔案
- 補充
-
- 切入點表達式可以通過操作符&& || ! 結合起來,更加的靈活
- 引入通知
spring 切面筆記
切面AOP
使用AOP 的好處 :
- 每個事物邏輯位于一個位置,代碼不分散,便于維護和更新
- 業務子產品更簡潔,隻包含核心業務代碼
例子 :
3. 驗證參數
4. 前置日志
5. 後置日志
切面術語
- 切面AOP : 橫切關注點(跨越應用程式多個子產品的功能)被子產品化的特殊對象
- 通知Adivice : 切面必須完成的工作
- 目标Target :被通知的對象
- 代理Proxy : 向目标對象應用通知之後建立的對象
- 連接配接點Joinpoint : 程式執行的某個特定位置,如類某個方法調用前後,異常等。連接配接點由兩個資訊确定:方法表示的程式執行點表示的方位
- 切點pointcut : 每個類都擁有多個連接配接點,即 連接配接點是程式類中客觀存在的事務。AOP通過切點定位到特定的連接配接點。類比 : 連接配接點相當于資料庫中的記錄,切點相當于查詢
spring AOP 使用的都是AspectJ(兩種模式注解或配置檔案)
注解
注解支援
- 包,(不用說明)
- 配置
<!-- 使Aspjectj 注解起作用, :自動比對的類生成代理對象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
注解聲明切面
- 要在Spring中聲明AspectJ切面,隻需要在IOC容器中将切面聲明Bean執行個體
- 在AspectJ的注解中,切面隻是一個帶有@Aspect注解的java類
- 通知是标注有某種注解的簡單的java方法
- AspectJ支援5種類型的通知注解:具體的說明在下面的代碼中
- @Before 前置通知
- @After 後置通知
- @AfterRunning 傳回通知,方法傳回結果之後執行
- @AfterThrowing 異常通知
- @Around 環繞通知,(一般不使用,僅了解)
package com.spring.aop;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
//@Order 值越小,優先級越高
/**
* @author Firewine
*/
@Order(1)//表示優先級的意思,越小,優先級越高
@Aspect
@Component
public class LoggingAspect {
/*
* 定義一個方法:用于聲明切入點表達式,一般地,該方法種再不需要填入其他的代碼
* 使用@Pointcut 來聲明切入點表達式
* 後面的其他直接使用方法名來引用目前的切入點表達式
*/
@Pointcut("execution(public int com.spring.aop.ArithmeticCalculator.*(..))")
public void declareJoinPointException() {}
/*
* 在com.spring.aop.ArithmeticCalculator 接口的每一個實作類的每一個方法開始之前執行一段代碼
*
*/
@Before("declareJoinPointException()")
public void beforeMethd(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
Object [] args = joinPoint.getArgs();
System.out.println("The method "+ methodName+"begins with " + Arrays.asList(args));
}
/*
* 在方法執行之後執行的代碼,無論該方法是否出現異常
* * 号表示,是任意的方法,參數,包,類等等
*/
@After("execution(public int com.spring.aop.ArithmeticCalculator.*(..))")
public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The method "+ methodName+"ends " );
}
/*
* 在方法正常結束後執行的代碼
* 傳回通知是可以通路到方法的傳回值的
*
*
*/
@AfterReturning(value="execution(public int com.spring.aop.ArithmeticCalculator.*(..))",returning="result")
public void afterReturning(JoinPoint joinPoint , Object result) {
String methodName = joinPoint.getSignature().getName();
Object [] args = joinPoint.getArgs();
System.out.println("The method "+ methodName+"ends with" + result );
}
/**
* 在方法出現異常時,會執行的代碼
* 可以通路到異常對象,且可以指定在出現特定異常在執行通知代碼
* @param joinPoint
* @param ex
*/
@AfterThrowing(value="execution(public int com.spring.aop.ArithmeticCalculator.*(..))",
throwing="ex")
public void afterThrowing(JoinPoint joinPoint,Exception ex) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The method "+ methodName+"occurs excetion :" + ex );
}
/**
* 環繞通知需要攜帶 ProceedingJoinPoint 類型的參數
* 環繞通知類似于動态代理的全過程:ProceedingJoinPoint 這個類型的參數可以決定是否執行目标方法。
* 且環繞通知必須有傳回值,傳回值即為目标方法的傳回值
* @param pjd
*/
//這個不是很常用的
@Around("execution(public int com.spring.aop.ArithmeticCalculator.*(..))")
public Object aroundMethod(ProceedingJoinPoint pjd) {
System.out.println("aroundMethod");
Object result = null;
String methodName = pjd.getSignature().getName();
//執行目标方法
try{
//前置通知
System.out.println("The method "+ methodName + "begins with " + Arrays.asList(pjd.getArgs()));
result = pjd.proceed();
//後置通知
System.out.println("The method "+methodName+"ends with " + result);
}catch (Throwable e) {
//異常通知
System.out.println("the method occurs exception : "+ e);
e.printStackTrace();
}
//後置通知
System.out.println("The method " + methodName + "ends");
return result;
}
}
xml配置檔案
**
<!-- 配置切面的bean-->
<bean id="loggingAspect"
class="com.spring.aop.xml.LoggingAspect"></bean>
<bean id="vlidationAspect"
class="com.spring.aop.xml.VlidationAspect"></bean>
<!-- 配置AOP -->
<aop:config>
<!-- 配置切點表達式 -->
<aop:pointcut expression="execution(* com.spring.aop.xml.ArithmeticCalculator.*(int , int ))" id="pointcut"/>
<!-- 配置切面及通知 -->
<aop:aspect ref="loggingAspect" order="2">
<aop:before method="beforeMethd" pointcut-ref="pointcut"/>
<aop:after method="afterMethod" pointcut-ref="pointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="ex"/>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
</aop:aspect>
<aop:aspect ref="vlidationAspect" order="1">
<aop:before method="validateArgs" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
補充
切入點表達式可以通過操作符&& || ! 結合起來,更加的靈活
引入通知
是一種特殊的通知類型,它通過為接口提供實作類,容許對象動态的實作接口,就像對象在運作時擴充了實作 類一樣。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHLsFzVZ5GdtNmdk5WY1g2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLzYTO3UTOxADM3IjMwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
執行個體代碼
#### 兩種配置方式的優先級
==注解的形式優先于基于XML的生命