AOP编程过程中的JoinPoint接口

        在AOP编程过程中需要使用到的一个 JoinPoint 接口,用来获取调用的信息,下面是这个接口的代码

        

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.aspectj.lang;

import org.aspectj.lang.reflect.SourceLocation;

public interface JoinPoint {
    // 一堆接口的默认字符串变量,暂时不用管
    String METHOD_EXECUTION = "method-execution";
    String METHOD_CALL = "method-call";
    String CONSTRUCTOR_EXECUTION = "constructor-execution";
    String CONSTRUCTOR_CALL = "constructor-call";
    String FIELD_GET = "field-get";
    String FIELD_SET = "field-set";
    String STATICINITIALIZATION = "staticinitialization";
    String PREINITIALIZATION = "preinitialization";
    String INITIALIZATION = "initialization";
    String EXCEPTION_HANDLER = "exception-handler";
    String SYNCHRONIZATION_LOCK = "lock";
    String SYNCHRONIZATION_UNLOCK = "unlock";
    String ADVICE_EXECUTION = "adviceexecution";

    // 下面几个方法就是我们需要关注的

    // 打印,信息量一般
    String toString();
    // 打印,信息量少
    String toShortString();
    // 打印,信息量多
    String toLongString();
    // 获取代理对象
    Object getThis();
    //  获取被代理的对象
    Object getTarget();
    // 获取切入点获取参数
    Object[] getArgs();
    // Signature ,这是另一个需要着重注意的类,基本上也是获取切入点信息的
    Signature getSignature();

    // 切入点所在类文件中的位置
    SourceLocation getSourceLocation();
    // 切入点类型 猜测是上面定义的默认变量中的几个,比如 METHOD_EXECUTION等
    String getKind();
    // 调用静态方法
    JoinPoint.StaticPart getStaticPart();

    public interface EnclosingStaticPart extends JoinPoint.StaticPart {
    }
    
    // 展示有哪些静态方法,基本都是上面已有的,也暂时不用管
    public interface StaticPart {
        Signature getSignature();

        SourceLocation getSourceLocation();

        String getKind();

        int getId();

        String toString();

        String toShortString();

        String toLongString();
    }
}

我们主要关注的就是那几个方法,上面的默认变量不用太关注,下面是实际的测试信息

切面:

   @After(value = "execution(* com.mz.testSpring.controler.testSpringControler.sayHello(..))")
    public void afterMethod(JoinPoint joinPoint){
        System.out.println("I'm fine ,thank you");
        //JoinPoint测试
        System.out.println("toString:"+joinPoint.toString());
        System.out.println("toShortString:"+joinPoint.toShortString());
        System.out.println("toLongString:"+joinPoint.toLongString());
        System.out.println("getThis:"+joinPoint.getThis().toString());
        System.out.println("getTarget:"+joinPoint.getTarget().toString());
        System.out.println("getArgs:"+ Arrays.toString(joinPoint.getArgs()));
        System.out.println("getSignature:"+joinPoint.getSignature().toString());
        System.out.println("getSourceLocation1:"+joinPoint.getSourceLocation().toString());
        System.out.println("getSourceLocation3:"+joinPoint.getSourceLocation().getWithinType().getName());
        System.out.println("getKind:"+joinPoint.getKind());
        System.out.println("getStaticPart:"+joinPoint.getStaticPart());

    }

原始功能:

    @GetMapping(value = "/testAnno")
    public String sayHello(String name){
       return "Are you ok? "+name;
    }

调用URL:

GET http://localhost:8080/test/testAnno?name=Indian frends
Accept: application/json

httpClient返回结果(主要是测一下逻辑功能有没有问题,与AOP编程关系不大):

GET http://localhost:8080/test/testAnno?name=Indian+frends

HTTP/1.1 200 
Content-Type: application/json;charset=UTF-8
Content-Length: 25
Date: Mon, 06 Dec 2021 06:10:20 GMT

Are you ok? Indian frends

Response code: 200; Time: 60ms; Content length: 25 bytes

切面打印的结果:

I'm fine ,thank you
toString:execution(String com.mz.testSpring.controler.testSpringControler.sayHello(String))
toShortString:execution(testSpringControler.sayHello(..))
toLongString:execution(public java.lang.String com.mz.testSpring.controler.testSpringControler.sayHello(java.lang.String))
getThis:com.mz.testSpring.controler.testSpringControler@5368e981
getTarget:com.mz.testSpring.controler.testSpringControler@5368e981
getArgs:[Indian frends]
getSignature:String com.mz.testSpring.controler.testSpringControler.sayHello(String)
getSourceLocation1:org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$SourceLocationImpl@1aa60b10
getSourceLocation3:com.mz.testSpring.controler.testSpringControler
getKind:method-execution
getStaticPart:execution(String com.mz.testSpring.controler.testSpringControler.sayHello(String))

从返回结果我们可以看出来,JoinPoint可以获取原始功能的名称,所在的类,所在的包,参数等。我们获取这些信息之后就可以通过反射来对原始功能进行补充,实现功能的增强,也就是说AOP编程和反射是分不开的。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值