连接点对象 JoinPoint and ProceedingJoinPoint ,AOP 编程

先看代码 —— 先干为敬 !

package biz.baijing.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Slf4j
@Component
@Aspect
public class TryJoinPoint {
//
//    @Pointcut("@annotation(biz.baijing.aop.TryLogging)")
//    public void poct() {}

    @Pointcut("execution(* biz.baijing.service.impl.DeptServiceImpl.*(..))")
    public void poct() {}

    @Before("poct()")
    public void before(JoinPoint joinPoint) {
        log.info("before _ TryJoinPoint");
    }


    @Around("poct()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("around _ TryJoinPoint");
        // 获取目标对象的类名
        String className = joinPoint.getTarget().getClass().getName();
        log.info("around 类名 ClassName = " + className);
        // 获取目标对象的方法名
        String methodeDecName = joinPoint.getSignature().getDeclaringType().getName();
        log.info("around 方法dec MethodName = " + methodeDecName);
        String methodeName = joinPoint.getSignature().getName();
        log.info("around 方法 MethodName = " + methodeName);
        // 获取目标对象传递的参数 — 运行时 Runtime
        Object[] args = joinPoint.getArgs();
        log.info("around 参数 args = " + Arrays.toString(args));
        // 放行 目标方法执行
        Object proceed = joinPoint.proceed();
        log.info("around 返回值 proceed = " + proceed);

        log.info("Around After …… TryJoinPoint");
        return proceed;
    }

}

代码的注解,已经完成了对代码意义的说明;这里看执行结果。

biz.baijing.aop.TryJoinPoint             : around _ TryJoinPoint
biz.baijing.aop.TryJoinPoint             : around 类名 ClassName = biz.baijing.service.impl.DeptServiceImpl
biz.baijing.aop.TryJoinPoint             : around 方法dec MethodName = biz.baijing.service.impl.DeptServiceImpl
biz.baijing.aop.TryJoinPoint             : around 方法 MethodName = list
biz.baijing.aop.TryJoinPoint             : around 参数 args = []
biz.baijing.aop.TryJoinPoint             : before _ TryJoinPoint

进行了 select dept 操作。

around 返回值 proceed = [  ... ...  ]
// 。。。 。。。 是返回的 dept 数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值