這期內容當中小編將會給大家帶來有關Spring AOP中怎么實現面向切面編程,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
什么是面向切面編程?
面向切面編程是一種編程范式(其他常見的編程范式有 面向過程編程,面向對象編程OOP,面向函數編程,面向事件驅動編程,面向切面編程),它不是一種編程語言,面向切面編程可以解決特定的問題,但是不能解決所有問題,它是面向對象編程的補充,不是替代。
它可以很大程度上解決代碼重復性問題,而且可以實現關注點分離,比如功能性需求和非功能性需求的分離,從而實現集中管理,增強代碼的可讀性,可維護性。
在系統開發過程中常見的使用場景 主要有
權限控制
緩存控制
事務控制
審計日志
性能監控
分布式追蹤
異常處理
切面表達式,主要表達通過怎樣的方式找到切面插入的邏輯點,pointcut express 提供了豐富的表達式可以讓我們進行切面的插入。
找到切入點后,需要明確在什么時機進行代碼植入,主要有五種,如下:
@Before 前置通知
@After(finally) ,后置通知,在方法執行完之后切入
@AfterReturning,返回通知,返回值返回之后
@AfterThrowing,異常通知,拋出異常之后
@Around ,環繞通知,環繞通知包含了上面所有的類型
以上兩個關注點 總結一句話就是 在什么地方什么時機進行我們的代碼切入。
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* //匹配ProductService類里頭的所有方法
* @Pointcut("within(com.ruoli.service.ProductService)")
* //匹配com.ruoli包及子包下所有類的方法
* @Pointcut("within(com.ruoli..*)")
*/
@Aspect
@Component
public class PkgTypeAspectConfig {
@Pointcut("within(com.ruoli.service.sub.*)")
public void matchType(){}
@Before("matchType()")
public void before(){
System.out.println("");
System.out.println("###before");
}
}2、對象匹配
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* //匹配AOP對象的目標對象為指定類型的方法,即LogService的aop代理對象的方法
* @Pointcut("this(com.ruoli.log.Loggable)")
* //匹配實現Loggable接口的目標對象(而不是aop代理后的對象)的方法
* @Pointcut("target(com.ruoli.log.Loggable)")
* //this 可以攔截 DeclareParents(Introduction)
* //target 不攔截 DeclareParents(Introduction)
* //匹配所有以Service結尾的bean里頭的方法
* @Pointcut("bean(*Service)")
* Created by cat on 2016-12-04.
*/
@Aspect
@Component
public class ObjectAspectConfig {
@Pointcut("bean(logService)")
public void matchCondition(){}
@Before("matchCondition()")
public void before(){
System.out.println("");
System.out.println("###before");
}
}3、參數匹配,配置指定參數的方法
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* //匹配任何以find開頭而且只有一個Long參數的方法
* @Pointcut("execution(* *..find*(Long))")
* //匹配任何以find開頭的而且第一個參數為Long型的方法
* @Pointcut("execution(* *..find*(Long,..))")
* //匹配任何只有一個Long參數的方法
* @Pointcut("within(com.ruoli..*) && args(Long)")
* //匹配第一個參數為Long型的方法
* @Pointcut("within(com.ruoli..*) && args(Long,..)")
* Created by cat on 2016-12-04.
*/
@Aspect
@Component
public class ArgsAspectConfig {
@Pointcut("args(Long,String) && within(com.ruoli.service.*)")
public void matchArgs(){}
@Before("matchArgs()")
public void before(){
System.out.println("");
System.out.println("###before");
}
}4、注解匹配
主要有 方法級別注解,類級別注解,參數級別注解。
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* //匹配方法標注有AdminOnly的注解的方法
* @Pointcut("@annotation(com.ruoli.anno.AdminOnly) && within(com.ruoli..*)")
* //匹配標注有NeedSecured的類底下的方法 //class級別
* @Pointcut("@within(com.ruoli.anno.NeedSecured) && within(com.ruoli..*)")
* //匹配標注有NeedSecured的類及其子類的方法 //runtime級別
* 在spring context的環境下,二者沒有區別
* @Pointcut("@target(com.ruoli.anno.NeedSecured) && within(com.ruoli..*)")
* //匹配傳入的參數類標注有Repository注解的方法
* @Pointcut("@args(com.ruoli.anno.NeedSecured) && within(com.ruoli..*)")
* Created by cat on 2016-12-04.
*/
@Aspect
@Component
public class AnnoAspectConfig {
@Pointcut("@args(com.ruoli.anno.NeedSecured) && within(com.ruoli..*)")
public void matchAnno(){}
@Before("matchAnno()")
public void before(){
System.out.println("");
System.out.println("###before");
}
}5、execution 表達式
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* //匹配任何公共方法
@Pointcut("execution(public * com.ruoli.service.*.*(..))")
//匹配com.imooc包及子包下Service類中無參方法
@Pointcut("execution(* com.ruoli..*Service.*())")
//匹配com.imooc包及子包下Service類中的任何只有一個參數的方法
@Pointcut("execution(* com.ruoli..*Service.*(*))")
//匹配com.imooc包及子包下任何類的任何方法
@Pointcut("execution(* com.ruoli..*.*(..))")
//匹配com.imooc包及子包下返回值為String的任何方法
@Pointcut("execution(String com.ruoli..*.*(..))")
//匹配異常
execution(public * com.ruoli.service.*.*(..) throws java.lang.IllegalAccessException)
*
*/
@Aspect
@Component
public class ExecutionAspectConfig {
@Pointcut("execution(public * com.ruoli.service..*Service.*(..) throws java.lang.IllegalAccessException)")
public void matchCondition(){}
@Before("matchCondition()")
public void before(){
System.out.println("");
System.out.println("###before");
}
}5、五種通知代碼示例
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.omg.CORBA.Object;
import org.springframework.stereotype.Component;
/**
* @Before("matchAnno()")
* @After("matchAnno())") //相當于finally
* @AfterReturning("matchException()")
* @AfterThrowing("matchException()")
* @Around("matchException()")
* @Before(value = "matchLongArg() && args(productId)")
* public void beforeWithArgs(Long productId)
* @AfterReturning(value = "matchReturn()",returning = "returnValue")
* public void getReulst(Object returnValue)
*
*/
@Aspect
@Component
public class AdviceAspectConfig {
/******pointcut********/
@Pointcut("@annotation(com.ruoli.anno.AdminOnly) && within(com.ruoli..*)")
public void matchAnno(){}
@Pointcut("execution(* *..find*(Long)) && within(com.ruoli..*) ")
public void matchLongArg(){}
@Pointcut("execution(public * com.ruoli.service..*Service.*(..) throws java.lang.IllegalAccessException) && within(com.ruoli..*)")
public void matchException(){}
@Pointcut("execution(String com.ruoli..*.*(..)) && within(com.ruoli..*)")
public void matchReturn(){}
/******advice********/
@Before("matchLongArg() && args(productId)")
public void before(Long productId){
System.out.println("###before,get args:"+productId);
}
@Around("matchException()")
public java.lang.Object after(ProceedingJoinPoint joinPoint){
System.out.println("###before");
java.lang.Object result = null;
try{
result = joinPoint.proceed(joinPoint.getArgs());
System.out.println("###after returning");
}catch (Throwable e){
System.out.println("###ex");
//throw
e.printStackTrace();
}finally {
System.out.println("###finally");
}
return result;
}
}上述就是小編為大家分享的Spring AOP中怎么實現面向切面編程了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。