day10-AOP-03

来源:https://www.cnblogs.com/liyuelian/archive/2023/01/26/17068274.html
-Advertisement-
Play Games

AOP-03 7.AOP-切入表達式 7.1切入表達式的具體使用 1.切入表達式的作用: 通過表達式的方式定義一個或多個具體的連接點。 2.語法細節: (1)切入表達式的語法格式: execution([許可權修飾符] [返回值類型] [簡單類名/全類名] [方法名]([參數列表]) 若目標類、介面與 ...


AOP-03

7.AOP-切入表達式

7.1切入表達式的具體使用

1.切入表達式的作用:

通過表達式的方式定義一個或多個具體的連接點。

2.語法細節:

(1)切入表達式的語法格式:

execution([許可權修飾符] [返回值類型] [簡單類名/全類名] [方法名]([參數列表])

若目標類、介面與該切麵類在用同一個包中,可以省略包名,只寫簡單類名


(2)舉例說明:

例子1:

表達式:execution(* com.sina.spring.ArithmeticCalculator.*(..))

含義:ArithmeticCalculator.* :介面中聲明的所有方法。第一個 * 代表任意修飾符和任意返回值。 第二個 * 代表任意方法。.. 代表匹配任意數量和任意類型的參數,若目標類、介面與該切麵類在用同一個包中,可以省略包名。

例子2:

表達式:execution(public * ArithmeticCalculator.*(..))

含義:ArithmeticCalculator 介面中的所有公有方法

例子3:

表達式:execution(public double ArithmeticCalculator.*(..))

含義:ArithmeticCalculator 介面中返回 double 類型數值的方法

例子4:

表達式:execution(public double ArithmeticCalculator.*(double, ..))

含義:第一個參數為double 類型的方法。.. 匹配任意數量、任意類型的參數。

例子5:

表達式:execution(public double ArithmeticCalculator.*(double, double))

含義:參數類型為double ,double 類型的方法。


(3)在AspectJ中,切入點表達式可以通過 && 或者 || 或者 ! 等操作符結合起來

例子:

表達式:execution(* *.add(int, ..))|| exexution(* *.sub(int, ..))

含義:任意類中第一個參數為 int 類型的 add 方法或 sub 方法

7.2註意事項和細節

  1. 切入表達式可以指向(實現了介面的)類的方法,這時切入表達式會對該類/對象生效

  2. 切入表達式也可以指向介面的方法,這時切入表達式會對實現了介面的類/對象生效

  3. 切入表達式可以對沒有實現介面的類進行切入。

    這涉及到CGlib動態代理:動態代理jdk的Proxy與spring的CGlib

  4. 兩個動態代理的區別:

    • JDK動態代理是面向介面的,只能增強實現類中介面中存在的方法。CGlib是面向父類的,可以增強父類的所有方法

    • JDK得到的對象是JDK代理對象實例,而CGlib得到的對象是被代理對象的子類

靜態代理例子:

一個普通類Car:

package com.li.aop.hw;

import org.springframework.stereotype.Component;

/**
 * @author 李
 * @version 1.0
 */
@Component
public class Car {
    public void run() {
        System.out.println("小汽車在running...");
    }
}

MyAspect切麵類:

package com.li.aop.hw;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author 李
 * @version 1.0
 * 切麵類
 */

@Component
@Aspect
public class MyAspect {
    //CGlib
    //給沒有實現介面的一個普通類設置前置通知,其他通知亦可以設置
    @Before(value = "execution(public void Car.run())")
    public void ok(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("MyAspect前置通知ok()-目標方法-" + methodName +
                " 參數-" + Arrays.toString(joinPoint.getArgs()));
    }
}

測試類:

package com.li.aop.hw;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.Test;

/**
 * @author 李
 * @version 1.0
 */
public class UsbTest {
    @Test
    public void UsbAspectTest() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans08.xml");
        //carProxy是 被代理對象的子類
        Car carProxy = ioc.getBean(Car.class);
        System.out.println("carProxy的實際運行類型=" + carProxy.getClass());
        carProxy.run();
    }
}

測試結果:

image-20230125221222962

8.AOP-JoinPoint

在切麵類的通知方法中,可以通過 JoinPoint對象獲取到目標方法的一系列信息:

JoinPoint對象的方法 釋義
getSignature().getName() 獲取目標方法名
getSignature().getDeclaringType().getSimpleName() 獲取目標方法所屬類的簡單類名
getSignature().getDeclaringTypeName() 獲取目標方法所屬類的全類名
getSignature().getModifiers() 獲取目標方法聲明類型(public/private/protected)
getArgs() 獲取傳入目標方法的參數,返回一個數組
getTarget() 獲取被代理的對象
getThis() 獲取代理對象自己

9.AOP-返回通知獲取結果

  • 在返回通知 @AfterReturning 中,可以獲取目標方法返回的結果。

我們在 @AfterReturning (返回通知)的註解源碼中可以看到有一個returning屬性。通過 returning 屬性可以獲取目標方法執行完畢後,返回的結果。

底層大概是:在反射執行目標方法時,將目標方法返回的結果賦給 returning 的定義的變數,然後賦給切入方法同名的參數。

image-20230126000139909

例子

  1. 必須在返回通知中,才能獲取目標方法的返回值

  2. returning 屬性定義的變數,要和切入方法接收的參數名稱一致。

image-20230126001018740

10.AOP-異常通知中獲取異常

  • 在異常通知中,可以獲取目標方法出現異常的信息

異常通知 @AfterThrowing 中有一個throwing 的屬性,它可以接收異常信息

image-20230126173006726

例子

image-20230126173453865 image-20230126173656035

11.AOP-環繞通知

  • 環繞通知可以完成其他四個通知要做的事情(瞭解即可)

以AOP-02-6.2 快速入門為例子

介面為 SmartAnimal.java,實現類為 SmartDog.java

切麵類為 SmartAnimalAspect2.java:

package com.li.aop.aspectj;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;


import java.util.Arrays;
import java.util.List;

/**
 * @author 李
 * @version 1.0
 * 切麵類-這裡主要演示環繞通知
 */
@Aspect
@Component
public class SmartAnimalAspect2 {

    //環繞通知
    /**
     * 1.@Around 表示環繞通知,它可以完成其他四個通知的功能
     * 2.value = "execution(...)" 切入表達式
     * 3.doAround() 表示要切入的方法,調用結構為 try-catch-finally
     *
     * @param joinPoint 如果是環繞通知,需要用到 ProceedingJoinPoint
     * @return
     */
    @Around(value = "execution(public float com.li.aop.aspectj.SmartDog.getSum(float, float))")
    public Object doAround(ProceedingJoinPoint joinPoint) {
        Object result = null;
        String methodName = joinPoint.getSignature().getName();
        try {

            //1.相當於前置通知完成的事情
            Object[] args = joinPoint.getArgs();
            List<Object> argList = Arrays.asList(args);
            System.out.println("AOP 環繞通知 " + methodName + "方法開始了--參數有:" + argList);

            //在環繞通知中一定要調用 joinPoint.proceed()來執行目標方法
            result = joinPoint.proceed();

            //2.相當於返回通知完成的事情
            System.out.println("AOP 環繞通知 " + methodName + "方法結束了--結果是:" + result);

        } catch (Throwable throwable) {

            //3.相當於異常通知完成的事情
            System.out.println("AOP 環繞通知 " + methodName + "方法拋異常了--異常對象:" + throwable);
        } finally {

            //4.相當於最終通知完成的事情
            System.out.println("AOP 環繞通知 " + methodName + "方法最終結束了...");
        }
        return result;
    }

}

測試方法:

@Test
public void testDoAround(){
    //得到ioc容器
    ApplicationContext ioc = new ClassPathXmlApplicationContext("beans07.xml");
    //獲取代理對象
    SmartAnimal smartAnimal = ioc.getBean(SmartAnimal.class);
    //執行方法
    smartAnimal.getSum(99,88);
}

測試結果:

image-20230126175900316

12.AOP-切入點表達式重用

  • 切入點表達式重用

    為了統一管理切入點表達式,可以使用切入點表達式重用/復用技術。

以AOP-02-6.2 快速入門為例子

介面為 SmartAnimal.java,實現類為 SmartDog.java

切麵類為 SmartAnimalAspect.java:

package com.li.aop.aspectj;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author 李
 * @version 1.0
 * 切麵類
 */
@Aspect 
@Component
public class SmartAnimalAspect {

    //定義一個切入點,在後面使用時可以直接引用,提高復用性
    @Pointcut(value = "execution(public float com.li.aop.aspectj.SmartDog.*(float, float))")
    public void myPointCut() {

    }

    //前置通知
    //@Before(value = "execution(public float com.li.aop.aspectj.SmartDog.*(float, float))")
    //這裡我們使用定義好的切入點
    @Before(value = "myPointCut()")
    public void f1(JoinPoint joinPoint) {
        //通過連接點對象joinPoint 拿到方法簽名
        Signature signature = joinPoint.getSignature();
        System.out.println("切麵類f1()-方法執行開始-日誌-方法名-" + signature.getName() +
                "-參數 " + Arrays.toString(joinPoint.getArgs()));
    }

    //返回通知
    //@AfterReturning(value = "execution(public float com.li.aop.aspectj.SmartDog.*(float, float))", returning = "res")
    @AfterReturning(value = "myPointCut()", returning = "res")
    public void f2(JoinPoint joinPoint, Object res) {
        Signature signature = joinPoint.getSignature();
        System.out.println("切麵類f2()-方法執行正常結束-日誌-方法名-" + signature.getName());
        System.out.println("目標方法返回的結果=" + res);
    }

    //異常通知
    //@AfterThrowing(value = "execution(public float com.li.aop.aspectj.SmartDog.*(float, float))", throwing = "throwable")
    @AfterThrowing(value = "myPointCut()", throwing = "throwable")
    public void f3(JoinPoint joinPoint, Throwable throwable) {
        Signature signature = joinPoint.getSignature();
        System.out.println("切麵類f3()-方法執行異常-日誌-方法名-" + signature.getName() + "異常信息-" + throwable);
    }

    //最終通知
    //@After(value = "execution(public float com.li.aop.aspectj.SmartDog.*(float, float))")
    @After(value = "myPointCut()")
    public void f4(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println("切麵類f4()-方法最終執行完畢-日誌-方法名-" + signature.getName());
    }

}

測試:

@Test
public void test(){
    //得到ioc容器
    ApplicationContext ioc = new ClassPathXmlApplicationContext("beans07.xml");
    //獲取代理對象
    SmartAnimal smartAnimal = ioc.getBean(SmartAnimal.class);
    //執行方法
    smartAnimal.getSum(99,88);
}

測試結果:

image-20230126181737265

13.AOP-切麵優先順序問題

如果同一個方法,有多個切麵在同一個切入點切入,那麼執行的優先順序如何控制?

答:在切麵類聲明時,使用@Order註解控制優先順序:n值越小,優先順序越高

@Order(value=n) //n值越小,優先順序越高

例子-以AOP-02-6.2 快速入門為例子

介面為 SmartAnimal.java,實現類為 SmartDog.java,兩個切麵類: SmartAnimalAspect.java 和 SmartAnimalAspect2.java

SmartAnimalAspect.java:

package com.li.aop.aspectj;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author 李
 * @version 1.0
 * 切麵類1
 */
@Aspect 
@Component 
@Order(value = 2)//表示該切麵類執行的順序,value越小,優先順序越高
public class SmartAnimalAspect {

    //定義一個切入點,在後面使用時可以直接引用,提高復用性
    @Pointcut(value = "execution(public float com.li.aop.aspectj.SmartDog.*(float, float))")
    public void myPointCut() {

    }

    //前置通知
    @Before(value = "myPointCut()")
    public void f1(JoinPoint joinPoint) {
        //通過連接點對象joinPoint 拿到方法簽名
        Signature signature = joinPoint.getSignature();
        System.out.println("切麵類1-f1()-方法執行開始-日誌-方法名-" + signature.getName() +
                "-參數 " + Arrays.toString(joinPoint.getArgs()));
    }

    //返回通知
    @AfterReturning(value = "myPointCut()", returning = "res")
    public void f2(JoinPoint joinPoint, Object res) {
        Signature signature = joinPoint.getSignature();
        System.out.println("切麵類1-f2()-方法執行正常結束-日誌-方法名-" + signature.getName() + " 目標方法返回的結果=" + res);
    }

    //異常通知
    @AfterThrowing(value = "myPointCut()", throwing = "throwable")
    public void f3(JoinPoint joinPoint, Throwable throwable) {
        Signature signature = joinPoint.getSignature();
        System.out.println("切麵類1-f3()-方法執行異常-日誌-方法名-" + signature.getName() + "異常信息-" + throwable);
    }

    //最終通知
    @After(value = "myPointCut()")
    public void f4(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println("切麵類1-f4()-方法最終執行完畢-日誌-方法名-" + signature.getName());
    }

}

SmartAnimalAspect2.java:

package com.li.aop.aspectj;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author 李
 * @version 1.0
 * 切麵類 2
 */
@Aspect
@Component
@Order(value = 1)
public class SmartAnimalAspect2 {

    @Before(value = "execution(public float com.li.aop.aspectj.SmartDog.*(float, float))")
    public void f1(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println("切麵類2-f1()-方法執行開始-日誌-方法名-" + signature.getName() +
                "-參數 " + Arrays.toString(joinPoint.getArgs()));
    }

    //返回通知:
    @AfterReturning(value = "execution(public float com.li.aop.aspectj.SmartDog.*(float, float))", returning = "res")
    public void f2(JoinPoint joinPoint, Object res) {
        Signature signature = joinPoint.getSignature();
        System.out.println("切麵類2-f2()-方法執行正常結束-日誌-方法名-" + signature.getName() + " 目標方法返回的結果=" + res);
    }

    //異常通知:
    @AfterThrowing(value = "execution(public float com.li.aop.aspectj.SmartDog.*(float, float))", throwing = "throwable")
    public void f3(JoinPoint joinPoint, Throwable throwable) {
        Signature signature = joinPoint.getSignature();
        System.out.println("切麵類2-f3()-方法執行異常-日誌-方法名-" + signature.getName() + "異常信息-" + throwable);
    }

    //最終通知:
    @After(value = "execution(public float com.li.aop.aspectj.SmartDog.*(float, float))")
    public void f4(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println("切麵類2-f4()-方法最終執行完畢-日誌-方法名-" + signature.getName());
    }

}

測試方法:

@Test
public void smartDogTestByAspectj() {
    //得到Spring容器
    ApplicationContext ioc =
            new ClassPathXmlApplicationContext("beans07.xml");
    //通過介面類型來獲得註入的SmartDog對象(實際上是代理對象proxy)
    SmartAnimal smartAnimal = ioc.getBean(SmartAnimal.class);
    smartAnimal.getSum(100, 48);
}

測試結果:

image-20230126193508557

註意事項和細節:

不能理解成:優先順序高的切麵類,每個消息通知都先執行。

真實的執行順序和 Filter 過濾器鏈式調用類似:

image-20230126193238162

14.基於XML配置AOP

前面我們都是通過註解來配置aop的,在spring中,同樣支持通過xml的方式來配置aop

例子

1.SmartAnimal 介面:

package com.li.aop.xml;

/**
 * @author 李
 * @version 1.0
 */
public interface SmartAnimal {
    //求和
    float getSum(float a, float b);

    //求差
    float getSub(float a, float b);
}

2.SmartDog 實現類:

package com.li.aop.xml;

/**
 * @author 李
 * @version 1.0
 */
public class SmartDog implements SmartAnimal {
    @Override
    public float getSum(float a, float b) {
        float result = a + b;
        System.out.println("方法內部列印 result = " + result);
        return result;
    }

    @Override
    public float getSub(float a, float b) {
        float result = a - b;
        System.out.println("方法內部列印 result = " + result);
        return result;
    }
}

3.SmartAnimalAspect 切麵類:

package com.li.aop.xml;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;

import java.util.Arrays;

/**
 * @author 李
 * @version 1.0
 * 這是一個切麵類,使用xml的方法配置
 */
public class SmartAnimalAspect {

    public void showBeginLog(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println("xml-showBeginLog()-方法執行開始-日誌-方法名-" + signature.getName() +
                "-參數 " + Arrays.toString(joinPoint.getArgs()));
    }

    public void showSuccessEndLog(JoinPoint joinPoint, Object res) {
        Signature signature = joinPoint.getSignature();
        System.out.println("xml-showSuccessEndLog()-方法執行正常結束-日誌-方法名-" + signature.getName() + " 目標方法返回的結果=" + res);
    }

    public void showExceptionLog(JoinPoint joinPoint, Throwable throwable) {
        Signature signature = joinPoint.getSignature();
        System.out.println("xml-showExceptionLog()-方法執行異常-日誌-方法名-" + signature.getName() + "異常信息-" + throwable);
    }

    public void showFinallyEndLog(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println("xml-showFinallyEndLog()-方法最終執行完畢-日誌-方法名-" + signature.getName());
    }

}

4.xml容器文件:

<?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
       https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--使用xml配置完成aop編程-->

    <!--配置一個切麵類對象 bean-->
    <bean class="com.li.aop.xml.SmartAnimalAspect" id="smartAnimalAspect"/>

    <!--配置一個SmartDog對象-->
    <bean class="com.li.aop.xml.SmartDog" id="smartDog"/>

    <!--配置切麵類 (註意要引入aop名稱空間)-->
    <aop:config>
        <!--先配置切入點-->
        <aop:pointcut id="myPointCut" expression="execution(public float com.li.aop.xml.SmartDog.*(float, float))"/>
        <!--配置切麵的前置/返回/異常/最終通知-->
        <aop:aspect ref="smartAnimalAspect" order="10">
            <!--配置前置通知-->
            <aop:before method="showBeginLog" pointcut-ref="myPointCut"/>
            <!--配置返回通知-->
            <aop:after-returning method="showSuccessEndLog" pointcut-ref="myPointCut" returning="res"/>
            <!--配置異常通知-->
            <aop:after-throwing method="showExceptionLog" pointcut-ref="myPointCut" throwing="throwable"/>
            <!--最終通知-->
            <aop:after method="showFinallyEndLog" pointcut-ref="myPointCut"/>
            <!--還可以配置環繞通知...-->
        </aop:aspect>
    </aop:config>

</beans>

5.測試類:

package com.li.aop.xml;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.Test;

/**
 * @author 李
 * @version 1.0
 * 測試類
 */
public class AopAspectjXMLTest {
    @Test
    public void testAspectjByXML() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans09.xml");
        SmartAnimal smartAnimalProxy = ioc.getBean(SmartAnimal.class);

        smartAnimalProxy.getSum(1000, 888);
    }
}

測試結果:

image-20230126202004918

15.aop練習

  1. 請編寫一個Cal介面,該介面有兩個方法:

    • cal1(int n),計算1 + 2 + ... + n
    • cal2(int n),計算1 * 2 *... * n
  2. Cal 實現類為 MyCal

  3. 請分別使用註解方式和xml配置的方式,完成aop編程

    • 在執行 cal2 前列印開始執行的時間,在執行完後列印時間
    • 在執行 cal2 前列印開始執行的時間,在執行完後列印時間

(1)xml配置的方式:

Cal:

package com.li.aop.hw2;

/**
 * @author 李
 * @version 1.0
 */
public interface Cal {
    //累加
    public void cal1(int n);

    //累乘
    public void cal2(int n);
}

MyCal:

package com.li.aop.hw2;

/**
 * @author 李
 * @version 1.0
 */
public class MyCal implements Cal {

    @Override
    public void cal1(int n) {
        int result = 0;
        if (n >= 1) {
            for (int i = 0; i <= n; i++) {
                result += i;
            }
            System.out.println("cal1-result=" + result);
            return;
        }
        System.out.println("cal1-參數有誤");
    }

    @Override
    public void cal2(int n) {
        int result = 1;
        if (n >= 1) {
            for (int i = 1; i <= n; i++) {
                result *= i;
            }
            System.out.println("cal2-result=" + result);
            return;
        }
        System.out.println("cal2-參數有誤");
    }
}

切麵類:

package com.li.aop.hw2;

/**
 * @author 李
 * @version 1.0
 * 切麵類
 */
public class CalAspect {
    //前置通知
    public void beforeTime() {
        System.out.println("開始執行計算 "+System.currentTimeMillis());
    }

    //返回通知
    public void returningTime() {
        System.out.println("結束執行計算 "+System.currentTimeMillis());
    }
}

xml容器配置文件:

<!--配置實現類對象bean-->
<bean class="com.li.aop.hw2.MyCal" id="myCal"/>
<!--配置切麵類對象bean-->
<bean class="com.li.aop.hw2.CalAspect" id="calAspect"/>
<aop:config>
    <!--配置切入點表達式-->
    <aop:pointcut id="myPointCut" expression="execution(public void com.li.aop.hw2.MyCal.*(int))"/>
    <!--配置切麵類-->
    <aop:aspect ref="calAspect">
        <!--前置通知-->
        <aop:before method="beforeTime" pointcut-ref="myPointCut"/>
        <!--返回通知-->
        <aop:after-returning method="returningTime" pointcut-ref="myPointCut"/>
    </aop:aspect>
</aop:config>

測試方法:

@Test
public void myCalTest() {
    ApplicationContext ioc = new ClassPathXmlApplicationContext("beans10.xml");
    //因為這裡只有一個實現對象,因此用類型獲取
    Cal myCalProxy = ioc.getBean(Cal.class);

    myCalProxy.cal1(200);
    System.out.println("===============");
    myCalProxy.cal2(5);
}

測試結果:

image-20230126210427948

(2)基於註解的配置方式

Cal 介面不變,在MyCal 實現類上添加 @Component 註解:

@Component
public class MyCal implements Cal {...}

切麵類:

package com.li.aop.hw2;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * @author 李
 * @version 1.0
 * 切麵類
 */
@Component
@Aspect
public class CalAspect {
    //前置通知
    //如果切麵類和目標類在同一個包,可以省略包名
    @Before(value = "execution(public void com.li.aop.hw2.Cal.*(int))")
    public void beforeTime(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println(signature.getName() + " 開始時間: " + System.currentTimeMillis());
    }

    //返回通知
    @AfterReturning(value = "execution(public void com.li.aop.hw2.Cal.*(int))")
    public void returningTime(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println(signature.getName() + " 結束時間: " + System.currentTimeMillis());
    }
}

xml容器配置文件:

<!--配置要掃描的包-->
<context:component-scan base-package="com.li.aop.hw2"/>
<!--開啟基於註解的aop功能-->
<aop:aspectj-autoproxy/>

測試方法不變:

@Test
public void myCalTest() {
    ApplicationContext ioc = new ClassPathXmlApplicationContext("beans10.xml");
    //因為這裡只有一個實現對象,因此用類型獲取
    //又因為是代理對象,因此使用介面類型獲取
    Cal myCalProxy = ioc.getBean(Cal.class);

    myCalProxy.cal1(200);
    System.out.println("===============");
    myCalProxy.cal2(5);
}

測試結果:

image-20230126212417533
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一道貪心演算法不是很明顯的題目,其實一般的遞推也可以做。 大體思路:肯定優先購買單價最低的奶農的牛奶,那麼就需要先根據牛奶單價進行排序,這裡用結構體會更好一點。之後在從前往後一個一個枚舉,直至購買的牛奶數量達到要求即可。 話不多說,上代碼: 1 #include<bits/stdc++.h> 2 us ...
  • 2023-01-24 一、NoSQL資料庫 1、NoSQL資料庫的簡介 NoSQL(NoSQL=Not Only SQL),即“不僅僅是SQL”,泛指非關係型的資料庫。NosQL不依賴業務邏輯方式存儲,而以簡單的key-value模式存儲。因此大大的增加了資料庫的擴展能力。 (1)不遵循SQL標準 ...
  • 前言 最近群里遇到獲取Route名為空的問題,當時沒在意。。。 直到自己在監控頁面啟動耗時,需要確定當前頁面是哪個從而方便標記它載入的耗時時,遇到同樣 route.settings.name 為空問題,模擬場景如下: 在 main.dart 頁面中點擊 + 按鈕跳轉到 TestPage2 頁面。 M ...
  • Lspatch的使用。xp模塊可以使用戶獲得應用原本所沒有的功能。使用模塊需要修改應用。Lspatch實現了無需Root修改應用。 ...
  • 前端面試題學習-HTML-個人總結 這是看別人總結的基礎上再度總結的,總結的鏈接如下 鏈接 1. DOCTYPE 的作用? 告知瀏覽器解析器用何標準解析文檔,若不指定則按相容模式進行解析(向後相容模擬老瀏覽器)。 IE5.5 引入的概念。 HTML5 之後無需指定,因為在之前的都是基於 SGML 的 ...
  • JavaScript 中有兩種類型轉換:隱式類型轉換和顯式類型轉換。 隱式類型轉換指 JavaScript 在運行時自動將一種類型轉換為另一種類型。例如,在數學運算中,JavaScript 會將字元串轉換為數字。 顯式類型轉換指在代碼中使用內置函數或全局對象將一種類型顯式地轉換為另一種類型。例如,使 ...
  • 商品系統是電商系統最基礎、最核心的系統之一。商品數據遍佈所有業務,首頁、門店頁、購物車、訂單、結算、售後、庫存、價格等,都離不開商品。商品信息要穩定提供至到家供應鏈的每個節點,所以必須要有一套穩定的、高性能的商品服務體系支撐。 隨著京東到家商品業務的快速發展,業務從單一轉變為多元化,系統功能設... ...
  • 測試一、虛繼承與繼承的區別 1.1 單個繼承,不帶虛函數 1>class B size(8): 1> + 1> 0 | + (base class A) 1> 0 | | _ia //4B 1> | + 1> 4 | _ib //4B 有兩個int類型數據成員,占8B,基類邏輯存在前面 1.2、單個 ...
一周排行
    -Advertisement-
    Play Games
  • Timer是什麼 Timer 是一種用於創建定期粒度行為的機制。 與標準的 .NET System.Threading.Timer 類相似,Orleans 的 Timer 允許在一段時間後執行特定的操作,或者在特定的時間間隔內重覆執行操作。 它在分散式系統中具有重要作用,特別是在處理需要周期性執行的 ...
  • 前言 相信很多做WPF開發的小伙伴都遇到過表格類的需求,雖然現有的Grid控制項也能實現,但是使用起來的體驗感並不好,比如要實現一個Excel中的表格效果,估計你能想到的第一個方法就是套Border控制項,用這種方法你需要控制每個Border的邊框,並且在一堆Bordr中找到Grid.Row,Grid. ...
  • .NET C#程式啟動閃退,目錄導致的問題 這是第2次踩這個坑了,很小的編程細節,容易忽略,所以寫個博客,分享給大家。 1.第一次坑:是windows 系統把程式運行成服務,找不到配置文件,原因是以服務運行它的工作目錄是在C:\Windows\System32 2.本次坑:WPF桌面程式通過註冊表設 ...
  • 在分散式系統中,數據的持久化是至關重要的一環。 Orleans 7 引入了強大的持久化功能,使得在分散式環境下管理數據變得更加輕鬆和可靠。 本文將介紹什麼是 Orleans 7 的持久化,如何設置它以及相應的代碼示例。 什麼是 Orleans 7 的持久化? Orleans 7 的持久化是指將 Or ...
  • 前言 .NET Feature Management 是一個用於管理應用程式功能的庫,它可以幫助開發人員在應用程式中輕鬆地添加、移除和管理功能。使用 Feature Management,開發人員可以根據不同用戶、環境或其他條件來動態地控制應用程式中的功能。這使得開發人員可以更靈活地管理應用程式的功 ...
  • 在 WPF 應用程式中,拖放操作是實現用戶交互的重要組成部分。通過拖放操作,用戶可以輕鬆地將數據從一個位置移動到另一個位置,或者將控制項從一個容器移動到另一個容器。然而,WPF 中預設的拖放操作可能並不是那麼好用。為瞭解決這個問題,我們可以自定義一個 Panel 來實現更簡單的拖拽操作。 自定義 Pa ...
  • 在實際使用中,由於涉及到不同編程語言之間互相調用,導致C++ 中的OpenCV與C#中的OpenCvSharp 圖像數據在不同編程語言之間難以有效傳遞。在本文中我們將結合OpenCvSharp源碼實現原理,探究兩種數據之間的通信方式。 ...
  • 一、前言 這是一篇搭建許可權管理系統的系列文章。 隨著網路的發展,信息安全對應任何企業來說都越發的重要,而本系列文章將和大家一起一步一步搭建一個全新的許可權管理系統。 說明:由於搭建一個全新的項目過於繁瑣,所有作者將挑選核心代碼和核心思路進行分享。 二、技術選擇 三、開始設計 1、自主搭建vue前端和. ...
  • Csharper中的表達式樹 這節課來瞭解一下表示式樹是什麼? 在C#中,表達式樹是一種數據結構,它可以表示一些代碼塊,如Lambda表達式或查詢表達式。表達式樹使你能夠查看和操作數據,就像你可以查看和操作代碼一樣。它們通常用於創建動態查詢和解析表達式。 一、認識表達式樹 為什麼要這樣說?它和委托有 ...
  • 在使用Django等框架來操作MySQL時,實際上底層還是通過Python來操作的,首先需要安裝一個驅動程式,在Python3中,驅動程式有多種選擇,比如有pymysql以及mysqlclient等。使用pip命令安裝mysqlclient失敗應如何解決? 安裝的python版本說明 機器同時安裝了 ...