五種通知方式來(lái)實(shí)現(xiàn)aop

五種通知方式來(lái)實(shí)現(xiàn)aop

五種通知包括:

1. 前置通知,在業(yè)務(wù)方法之前執(zhí)行
2. 后置通知,在業(yè)務(wù)方法之后執(zhí)行
3. 環(huán)繞通知,同時(shí)在業(yè)務(wù)方法的前后執(zhí)行
4. 帶有返回值通知,可以拿到業(yè)務(wù)方法的返回值
5. 異常通知,可以捕獲業(yè)務(wù)方法中的異常對(duì)象
第一種方法實(shí)現(xiàn)配置文件配置
public class MyAspect {


    public void before(JoinPoint jp){

        System.out.println("-----------before---------------");
        System.out.println(jp.toString());
    }


    public void after(){

        System.out.println("------------after------------");
    }


    public Object around(ProceedingJoinPoint pjp){
        try{

            System.out.println("around before");
            Object obj=pjp.proceed();
            System.out.println("around after "+obj);

            return obj;
        }catch (Throwable throwable){
            throwable.printStackTrace();
        }
        return null;
    }


    public void myReture(JoinPoint JP,Object obj){
        System.out.println("this is myReturn " +obj);
    }

    public void myThrow(JoinPoint jp,Throwable e){
        System.out.println("this is myThrow " +e.getMessage());
    }
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">


    <bean id="us" class="com.xj.aop05.UserService"/>
    <bean id="ma" class="com.xj.aop05.MyAspect"/>

    <aop:config proxy-target-class="true">
        <aop:aspect ref="ma">
            <aop:pointcut id="mpc" expression="execution(* com.xj.aop05.*.*(..))"/>
            
            <aop:before method="before" pointcut-ref="mpc"/>
            <aop:after method="after" pointcut-ref="mpc"/>
            <aop:around method="around" pointcut-ref="mpc"/>
            <aop:after-returning method="myReture" pointcut-ref="mpc" returning="obj"/>
            <aop:after-throwing method="myThrow" pointcut-ref="mpc" throwing="e"/>
        </aop:aspect>
    </aop:config>
</beans>

注意:如果同時(shí)配置所有的通知方式,則指向順序依次為:

before>around before >業(yè)務(wù)方法>after returning >around after >after

before>around before>業(yè)務(wù)方法>after throwing>after

第二種注釋

**
 *  以注解的方式實(shí)現(xiàn)的切面類MyAspect
 *
 *      當(dāng)前類中的五種通知方式均以注解方式完成
 */
@Component          //  標(biāo)注當(dāng)前類為一個(gè)組件
@Aspect             //  標(biāo)注當(dāng)前類為一個(gè)切面類
public class MyAspect {

    /**
     * @Pointcut 注解為了避免相同的匹配規(guī)則被定義多處,專門(mén)定義該方法設(shè)置執(zhí)行的匹配規(guī)則,各個(gè)自行調(diào)用即可
     *    write once, only once
     */
    @Pointcut(value = "execution(* com.qfedu.aop06.*.*(..))")
    public void setAll(){}


    /**
     * @Before 表示該方法為一個(gè)前置通知
     * @param jp 連接點(diǎn)
     */
    @Before("setAll()")
    public void myBefore(JoinPoint jp){
        //System.out.println(jp.getArgs());
        System.out.println("this is before.");
    }

    /**
     * @After 表示該方法為一個(gè)后置通知
     * @param jp 連接點(diǎn)
     */
    @After("setAll()")
    public void myAfter(JoinPoint jp){
        //System.out.println(jp.getArgs());
        System.out.println("this is after.");
    }

    /**
     * @Around 表示該方法為一個(gè)環(huán)繞通知
     * @param pjp 處理連接點(diǎn)
     * @return 返回每個(gè)業(yè)務(wù)方法的返回值
     */
    @Around("setAll()")
    public Object myAround(ProceedingJoinPoint pjp){
        Object obj = null;
        try {
            System.out.println("this is around before");

            obj = pjp.proceed();


            System.out.println("this is around after");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        return obj;
    }

    /**
     * @AfterReturning 表示該方法為一個(gè)帶有返回值的通知
     * @param jp 連接點(diǎn)
     * @param obj 業(yè)務(wù)方法的返回值
     */
    @AfterReturning(value = "setAll()", returning = "obj")
    public void myReturn(JoinPoint jp, Object obj){
        System.out.println("this is after returnning " + obj);
    }

    /**
     * @AfterThrowing 表示該方法為一個(gè)帶有異常的通知
     * @param jp 連接點(diǎn)
     * @param e Throwable對(duì)象
     */
    @AfterThrowing(value = "setAll()", throwing = "e")
    public void myThrowing(JoinPoint jp, Throwable e){
        System.out.println("this is after throwing " + e.getMessage());
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--
    context:component-scan 組件掃描
        base-package指定要掃描的包的路徑
    -->
    <context:component-scan base-package="com.qfedu.aop06" />

    <!--aop:aspectj-autoproxy標(biāo)簽實(shí)現(xiàn)自動(dòng)代理-->
    <aop:aspectj-autoproxy />
</beans>

注:使用注解的話是環(huán)繞通知proceed方法之前部分先執(zhí)行,使用xml配置的話取決于aop:before和aop:around的配置順序

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容