最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot?AOP中JoinPoint的使用方式和通知切點(diǎn)表達(dá)式

 更新時(shí)間:2024年05月13日 09:16:40   作者:程序員三時(shí)  
這篇文章主要介紹了SpringBoot?AOP中JoinPoint的使用方式和通知切點(diǎn)表達(dá)式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

JoinPoint和ProceedingJoinPoint對象

  • JoinPoint對象封裝了SpringAop中切面方法的信息,在切面方法中添加JoinPoint參數(shù),就可以獲取到封裝了該方法信息的JoinPoint對象.
  • ProceedingJoinPoint對象是JoinPoint的子接口,該對象只用在@Around的切面方法中
@Aspect
@Component
public class aopAspect {
    /**
     * 定義一個切入點(diǎn)表達(dá)式,用來確定哪些類需要代理
     * execution(* aopdemo.*.*(..))代表aopdemo包下所有類的所有方法都會被代理
     */
    @Pointcut("execution(* aopdemo.*.*(..))")
    public void declareJoinPointerExpression() {}

    /**
     * 前置方法,在目標(biāo)方法執(zhí)行前執(zhí)行
     * @param joinPoint 封裝了代理方法信息的對象,若用不到則可以忽略不寫
     */
    @Before("declareJoinPointerExpression()")
    public void beforeMethod(JoinPoint joinPoint){
        System.out.println("目標(biāo)方法名為:" + joinPoint.getSignature().getName());
        System.out.println("目標(biāo)方法所屬類的簡單類名:" +        joinPoint.getSignature().getDeclaringType().getSimpleName());
        System.out.println("目標(biāo)方法所屬類的類名:" + joinPoint.getSignature().getDeclaringTypeName());
        System.out.println("目標(biāo)方法聲明類型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));
        //獲取傳入目標(biāo)方法的參數(shù)
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            System.out.println("第" + (i+1) + "個參數(shù)為:" + args[i]);
        }
        System.out.println("被代理的對象:" + joinPoint.getTarget());
        System.out.println("代理對象自己:" + joinPoint.getThis());
    }

    /**
     * 環(huán)繞方法,可自定義目標(biāo)方法執(zhí)行的時(shí)機(jī)
     * @param pjd JoinPoint的子接口,添加了
     *            Object proceed() throws Throwable 執(zhí)行目標(biāo)方法
     *            Object proceed(Object[] var1) throws Throwable 傳入的新的參數(shù)去執(zhí)行目標(biāo)方法
     *            兩個方法
     * @return 此方法需要返回值,返回值視為目標(biāo)方法的返回值
     */
    @Around("declareJoinPointerExpression()")
    public Object aroundMethod(ProceedingJoinPoint pjd){
        Object result = null;

        try {
            //前置通知
            System.out.println("目標(biāo)方法執(zhí)行前...");
            //執(zhí)行目標(biāo)方法
            //result = pjd.proeed();
            //用新的參數(shù)值執(zhí)行目標(biāo)方法
            result = pjd.proceed(new Object[]{"newSpring","newAop"});
            //返回通知
            System.out.println("目標(biāo)方法返回結(jié)果后...");
        } catch (Throwable e) {
            //異常通知
            System.out.println("執(zhí)行目標(biāo)方法異常后...");
            throw new RuntimeException(e);
        }
        //后置通知
        System.out.println("目標(biāo)方法執(zhí)行后...");

        return result;
    }
}

切點(diǎn)表達(dá)式

  • 在Spring AOP中,連接點(diǎn)始終代表方法的執(zhí)行。切入點(diǎn)是與連接點(diǎn)匹配的,切入點(diǎn)表達(dá)語言是以編程方式描述切入點(diǎn)的方式。
  • 切入點(diǎn)(Poincut)是定義了在“什么地方”進(jìn)行切入,哪些連接點(diǎn)會得到通知。顯然,切點(diǎn)一定是連接點(diǎn)
  • 切點(diǎn)是通過@Pointcut注解和切點(diǎn)表達(dá)式定義的。@Pointcut注解可以在一個切面內(nèi)定義可重用的切點(diǎn)。

execute表達(dá)式

*代表匹配任意修飾符及任意返回值,參數(shù)列表中..匹配任意數(shù)量的參數(shù)

可以使用&&、||、!、三種運(yùn)算符來組合切點(diǎn)表達(dá)式,表示與或非的關(guān)系

  • 1.攔截任意公共方法execution(public * *(..))
  • 2.攔截以set開頭的任意方法execution(* set*(..))
  • 3.攔截類或者接口中的方法
攔截AccountService(類、接口)中定義的所有方法
execution(* com.xyz.service.AccountService.*(..))

4.攔截包中定義的方法,不包含子包中的方法

攔截com.xyz.service包中所有類中任意方法,**不包含**子包中的類
execution(* com.xyz.service.*.*(..))

5.攔截包或者子包中定義的方法

攔截com.xyz.service包或者子包中定義的所有方法
execution(* com.xyz.service..*.*(..))

通知分類

@Before

  • 前置通知: 在方法執(zhí)行之前執(zhí)行
  • 前置通知使用@Before注解 將切入點(diǎn)表達(dá)式值作為注解的值

@After

  • 后置通知, 在方法執(zhí)行之后執(zhí)行
  • 后置通知使用@After注解 ,在后置通知中,不能訪問目標(biāo)方法執(zhí)行的結(jié)果

@AfterRunning

  • 返回通知, 在方法返回結(jié)果之后執(zhí)行
  • 返回通知使用@AfterRunning注解

@AfterThrowing

  • 異常通知, 在方法拋出異常之后執(zhí)行
  • 異常通知使用@AfterThrowing注解

@Around

  • 環(huán)繞通知, 圍繞著方法執(zhí)行
  • 環(huán)繞通知使用@Around注解

package com.jason.spring.aop.impl;
 
import java.util.Arrays;
import java.util.List;
 
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.springframework.stereotype.Component;
 
 
//把這個類聲明為一個切面
//1.需要將該類放入到IOC 容器中
@Component
//2.再聲明為一個切面
@Aspect
public class LoggingAspect {
    
    //聲明該方法是一個前置通知:在目標(biāo)方法開始之前執(zhí)行 哪些類,哪些方法
    //作用:@before 當(dāng)調(diào)用目標(biāo)方法,而目標(biāo)方法與注解聲明的方法相匹配的時(shí)候,aop框架會自動的為那個方法所在的類生成一個代理對象,在目標(biāo)方法執(zhí)行之前,執(zhí)行注解的方法
    //支持通配符
    //@Before("execution(public int com.jason.spring.aop.impl.ArithmeticCaculatorImpl.*(int, int))")
    @Before("execution(* com.jason.spring.aop.impl.*.*(int, int))")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method " + methodName + " begins " + args);
    }
    
    /**
     * @Description:  在方法執(zhí)行后執(zhí)行的代碼,無論該方法是否出現(xiàn)異常
     * @param joinPoint
     */
    @After("execution(* com.jason.spring.aop.impl.*.*(int, int))")
    public void afterMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method " + methodName + " end " + args);
    }
    
    /**
     * 
     * @Description:  在方法正常結(jié)束后執(zhí)行代碼,放回通知是可以訪問到方法的返回值
     *
     * @param joinPoint
     */
    @AfterReturning( value="execution(* com.jason.spring.aop.impl.*.*(..))", returning="result")
    public void afterReturning(JoinPoint joinPoint ,Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " end with " + result);
    }
    
    /**
     * 
     * @Description:  在目標(biāo)方法出現(xiàn)異常時(shí)會執(zhí)行代碼,可以訪問到異常對象,且,可以指定出現(xiàn)特定異常時(shí)執(zhí)行通知代碼
     *
     * @param joinPoint
     * @param ex
     */
    @AfterThrowing(value="execution(* com.jason.spring.aop.impl.*.*(..))",throwing="ex")
    public void afterThrowting(JoinPoint joinPoint, Exception  ex){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " occurs exceptions " + ex);
    }
    
    /**
     * 
     * @Description: 環(huán)繞通知需要攜帶 ProceedingJoinPoint 類型的參數(shù)
     *                    環(huán)繞通知 類似于  動態(tài)代理的全過程
     *                   ProceedingJoinPoint:可以決定是否執(zhí)行目標(biāo)方法
     *    環(huán)繞通知必須有返回值,返回值即為目標(biāo)方法的返回值
     *    
     * @param proceedingJoinPoint
     */
    @Around("execution(* com.jason.spring.aop.impl.*.*(..))")
    public Object around(ProceedingJoinPoint proceedingJoinPoint){
        
        Object result = null;
        String methodName = proceedingJoinPoint.getSignature().getName();
        
        //執(zhí)行目標(biāo)方法
        try {
            //前置通知
            System.out.println("The method " + methodName + "begin with" + Arrays.asList(proceedingJoinPoint.getArgs()));
            
            result = proceedingJoinPoint.proceed();
            
            //后置通知
            System.out.println("The method " + methodName + "end with" + result);
            
        } catch (Throwable e) {
            //異常通知
            System.out.println("The method occurs exception : " + e);
            throw new RuntimeException();
        }
            //后置通知
            
        System.out.println("The method " + methodName + "end with" + result);
        
        return result;        
    }
}

切點(diǎn)表達(dá)式參考

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java的Array,List和byte[],String相互轉(zhuǎn)換的方法你了解嘛

    java的Array,List和byte[],String相互轉(zhuǎn)換的方法你了解嘛

    這篇文章主要為大家詳細(xì)介紹了java的Array,List和byte[],String相互轉(zhuǎn)換的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • SpringBoot事務(wù)失效的八大原因及解決方案

    SpringBoot事務(wù)失效的八大原因及解決方案

    在 Spring Boot 項(xiàng)目開發(fā)中,聲明式事務(wù)管理通過 @Transactional 注解提供了極大的便利,但許多開發(fā)者都曾遇到過事務(wù)不生效的困擾,本文將詳細(xì)分析導(dǎo)致 Spring Boot 事務(wù)失效的八大常見情況,并提供相應(yīng)的解決方案,需要的朋友可以參考下
    2025-09-09
  • Java多線程程序中synchronized修飾方法的使用實(shí)例

    Java多線程程序中synchronized修飾方法的使用實(shí)例

    synchronized關(guān)鍵字主要北用來進(jìn)行線程同步,這里我們主要來演示Java多線程程序中synchronized修飾方法的使用實(shí)例,需要的朋友可以參考下:
    2016-06-06
  • 最新評論

    沽源县| 闽清县| 临夏市| 隆尧县| 伊春市| 鄂伦春自治旗| 石家庄市| 印江| 通化市| 兴国县| 玉环县| 霍林郭勒市| 克什克腾旗| 碌曲县| 澎湖县| 习水县| 衡阳县| 萝北县| 大渡口区| 方正县| 米脂县| 株洲县| 庆元县| 合江县| 庄浪县| 阿鲁科尔沁旗| 剑河县| 涡阳县| 西平县| 红原县| 紫阳县| 隆子县| 铜山县| 上虞市| 柞水县| 镇江市| 长兴县| 阜新市| 汝阳县| 民丰县| 那曲县|