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
  • 概述:在C#中,++i和i++都是自增運算符,其中++i先增加值再返回,而i++先返回值再增加。應用場景根據需求選擇,首碼適合先增後用,尾碼適合先用後增。詳細示例提供清晰的代碼演示這兩者的操作時機和實際應用。 在C#中,++i 和 i++ 都是自增運算符,但它們在操作上有細微的差異,主要體現在操作的 ...
  • 上次發佈了:Taurus.MVC 性能壓力測試(ap 壓測 和 linux 下wrk 壓測):.NET Core 版本,今天計劃準備壓測一下 .NET 版本,來測試並記錄一下 Taurus.MVC 框架在 .NET 版本的性能,以便後續持續優化改進。 為了方便對比,本文章的電腦環境和測試思路,儘量和... ...
  • .NET WebAPI作為一種構建RESTful服務的強大工具,為開發者提供了便捷的方式來定義、處理HTTP請求並返迴響應。在設計API介面時,正確地接收和解析客戶端發送的數據至關重要。.NET WebAPI提供了一系列特性,如[FromRoute]、[FromQuery]和[FromBody],用 ...
  • 原因:我之所以想做這個項目,是因為在之前查找關於C#/WPF相關資料時,我發現講解圖像濾鏡的資源非常稀缺。此外,我註意到許多現有的開源庫主要基於CPU進行圖像渲染。這種方式在處理大量圖像時,會導致CPU的渲染負擔過重。因此,我將在下文中介紹如何通過GPU渲染來有效實現圖像的各種濾鏡效果。 生成的效果 ...
  • 引言 上一章我們介紹了在xUnit單元測試中用xUnit.DependencyInject來使用依賴註入,上一章我們的Sample.Repository倉儲層有一個批量註入的介面沒有做單元測試,今天用這個示例來演示一下如何用Bogus創建模擬數據 ,和 EFCore 的種子數據生成 Bogus 的優 ...
  • 一、前言 在自己的項目中,涉及到實時心率曲線的繪製,項目上的曲線繪製,一般很難找到能直接用的第三方庫,而且有些還是定製化的功能,所以還是自己繪製比較方便。很多人一聽到自己畫就害怕,感覺很難,今天就分享一個完整的實時心率數據繪製心率曲線圖的例子;之前的博客也分享給DrawingVisual繪製曲線的方 ...
  • 如果你在自定義的 Main 方法中直接使用 App 類並啟動應用程式,但發現 App.xaml 中定義的資源沒有被正確載入,那麼問題可能在於如何正確配置 App.xaml 與你的 App 類的交互。 確保 App.xaml 文件中的 x:Class 屬性正確指向你的 App 類。這樣,當你創建 Ap ...
  • 一:背景 1. 講故事 上個月有個朋友在微信上找到我,說他們的軟體在客戶那邊隔幾天就要崩潰一次,一直都沒有找到原因,讓我幫忙看下怎麼回事,確實工控類的軟體環境複雜難搞,朋友手上有一個崩潰的dump,剛好丟給我來分析一下。 二:WinDbg分析 1. 程式為什麼會崩潰 windbg 有一個厲害之處在於 ...
  • 前言 .NET生態中有許多依賴註入容器。在大多數情況下,微軟提供的內置容器在易用性和性能方面都非常優秀。外加ASP.NET Core預設使用內置容器,使用很方便。 但是筆者在使用中一直有一個頭疼的問題:服務工廠無法提供請求的服務類型相關的信息。這在一般情況下並沒有影響,但是內置容器支持註冊開放泛型服 ...
  • 一、前言 在項目開發過程中,DataGrid是經常使用到的一個數據展示控制項,而通常表格的最後一列是作為操作列存在,比如會有編輯、刪除等功能按鈕。但WPF的原始DataGrid中,預設只支持固定左側列,這跟大家習慣性操作列放最後不符,今天就來介紹一種簡單的方式實現固定右側列。(這裡的實現方式參考的大佬 ...