java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > AOP编程的基本概念与idea编辑器的配合

AOP编程的基本概念与idea编辑器的配合体验过程

作者:RR1335

文章简要介绍了AOP基础概念,包括Before/Around通知、PointCut切入点、Advice通知体、JoinPoint连接点等,说明它们如何通过切点表达式(如*、..)关联目标方法(DeptServiceImpl),实现公共逻辑抽取与模块化

初始化一个基础的 AOP 程序

// 作为演示程序,没有更具体的方法体

@Slf4j
@Component
@Aspect
public class TryAspect {

    @Before("execution(* biz.baijing.service.impl.DeptServiceImpl.*(..))")      // * 任意方法 , .. 形参任意
    public void before(){
        log.info("Before ...");
    }

    public void after(){
        log.info("After ...");
    }

    public void around(){
        log.info("Around before ...");

        // 调用目标方法


        log.info("around after");
    }

    public void afterReturning(){
        log.info("AfterReturning ...");
    }

    public void afterThrowing(){
        log.info("After Throwing ...");
    }

}

Before

注意编辑器这个位置的 m 的变化。

点击 m 提示了 

@Before("execution(* biz.baijing.service.impl.DeptServiceImpl.*(..))") 

切面目标的方法。

Around

before 是半圆;around 是完整的圆形。

同样点击会出现 execution 的指向的目标方法

注解: m = methods

对 around 方法,完善之后

    @Around("execution(* biz.baijing.service.impl.DeptServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint proceedingjoinPoint) throws Throwable{
        log.info("Around before ...");

        // 调用目标方法
        Object proceed = proceedingjoinPoint.proceed();

        log.info("around after");
        return proceed;
    }

Advise — 通知

方法体,是抽取的一组公共的实现逻辑。

        log.info("Around before ...");

        // 调用目标方法
        Object proceed = proceedingjoinPoint.proceed();

        log.info("around after");
        return proceed;

PointCut — 切入点

方法实际针对的目标方法的引入,是AOP方法和目标类方法的连接;

通过切入点表达式关联。

"execution(* biz.baijing.service.impl.DeptServiceImpl.*(..))"

备注: *  : 所有 DeptServiceImpl 下的方法 ; .. : 形参任意。

Acpect — 切面

advise 和 pointcut 的连接。

    @Around("execution(* biz.baijing.service.impl.DeptServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint proceedingjoinPoint) throws Throwable{
        log.info("Around before ...");

        // 调用目标方法
        Object proceed = proceedingjoinPoint.proceed();

        log.info("around after");
        return proceed;
    }

描述这个方法的类,就叫: 切面类。

@Slf4j
@Component
@Aspect
public class TryAspect {

    @Before("execution(* biz.baijing.service.impl.DeptServiceImpl.*(..))")      // * 任意方法 , .. 形参任意
    public void before(){
        log.info("Before ...");
    }

    public void after(){
        log.info("After ...");
    }

    @Around("execution(* biz.baijing.service.impl.DeptServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint proceedingjoinPoint) throws Throwable{
        log.info("Around before ...");

        // 调用目标方法
        Object proceed = proceedingjoinPoint.proceed();

        log.info("around after");
        return proceed;
    }

    public void afterReturning(){
        log.info("AfterReturning ...");
    }

    public void afterThrowing(){
        log.info("After Throwing ...");
    }

}

TryAspect —— 切面类。

Target — 目标对象

biz.baijing.service.impl.DeptServiceImpl

DeptServiceImpl —— 就是目标对象。

JoinPoint — 连接点

被 AOP 控制的对象方法,即:

biz.baijing.service.impl.DeptServiceImpl

下的所有方法

这里被 红色 m 标识的方法就是 JoinPoint 。

结果

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文