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
  • 1、預覽地址:http://139.155.137.144:9012 2、qq群:801913255 一、前言 隨著網路的發展,企業對於信息系統數據的保密工作愈發重視,不同身份、角色對於數據的訪問許可權都應該大相徑庭。 列如 1、不同登錄人員對一個數據列表的可見度是不一樣的,如數據列、數據行、數據按鈕 ...
  • 前言 上一篇文章寫瞭如何使用RabbitMQ做個簡單的發送郵件項目,然後評論也是比較多,也是準備去學習一下如何確保RabbitMQ的消息可靠性,但是由於時間原因,先來說說設計模式中的簡單工廠模式吧! 在瞭解簡單工廠模式之前,我們要知道C#是一款面向對象的高級程式語言。它有3大特性,封裝、繼承、多態。 ...
  • Nodify學習 一:介紹與使用 - 可樂_加冰 - 博客園 (cnblogs.com) Nodify學習 二:添加節點 - 可樂_加冰 - 博客園 (cnblogs.com) 介紹 Nodify是一個WPF基於節點的編輯器控制項,其中包含一系列節點、連接和連接器組件,旨在簡化構建基於節點的工具的過程 ...
  • 創建一個webapi項目做測試使用。 創建新控制器,搭建一個基礎框架,包括獲取當天日期、wiki的請求地址等 創建一個Http請求幫助類以及方法,用於獲取指定URL的信息 使用http請求訪問指定url,先運行一下,看看返回的內容。內容如圖右邊所示,實際上是一個Json數據。我們主要解析 大事記 部 ...
  • 最近在不少自媒體上看到有關.NET與C#的資訊與評價,感覺大家對.NET與C#還是不太瞭解,尤其是對2016年6月發佈的跨平臺.NET Core 1.0,更是知之甚少。在考慮一番之後,還是決定寫點東西總結一下,也回顧一下.NET的發展歷史。 首先,你沒看錯,.NET是跨平臺的,可以在Windows、 ...
  • Nodify學習 一:介紹與使用 - 可樂_加冰 - 博客園 (cnblogs.com) Nodify學習 二:添加節點 - 可樂_加冰 - 博客園 (cnblogs.com) 添加節點(nodes) 通過上一篇我們已經創建好了編輯器實例現在我們為編輯器添加一個節點 添加model和viewmode ...
  • 前言 資料庫併發,數據審計和軟刪除一直是數據持久化方面的經典問題。早些時候,這些工作需要手寫複雜的SQL或者通過存儲過程和觸發器實現。手寫複雜SQL對軟體可維護性構成了相當大的挑戰,隨著SQL字數的變多,用到的嵌套和複雜語法增加,可讀性和可維護性的難度是幾何級暴漲。因此如何在實現功能的同時控制這些S ...
  • 類型檢查和轉換:當你需要檢查對象是否為特定類型,並且希望在同一時間內將其轉換為那個類型時,模式匹配提供了一種更簡潔的方式來完成這一任務,避免了使用傳統的as和is操作符後還需要進行額外的null檢查。 複雜條件邏輯:在處理複雜的條件邏輯時,特別是涉及到多個條件和類型的情況下,使用模式匹配可以使代碼更 ...
  • 在日常開發中,我們經常需要和文件打交道,特別是桌面開發,有時候就會需要載入大批量的文件,而且可能還會存在部分文件缺失的情況,那麼如何才能快速的判斷文件是否存在呢?如果處理不當的,且文件數量比較多的時候,可能會造成卡頓等情況,進而影響程式的使用體驗。今天就以一個簡單的小例子,簡述兩種不同的判斷文件是否... ...
  • 前言 資料庫併發,數據審計和軟刪除一直是數據持久化方面的經典問題。早些時候,這些工作需要手寫複雜的SQL或者通過存儲過程和觸發器實現。手寫複雜SQL對軟體可維護性構成了相當大的挑戰,隨著SQL字數的變多,用到的嵌套和複雜語法增加,可讀性和可維護性的難度是幾何級暴漲。因此如何在實現功能的同時控制這些S ...