美文网首页
Spring AOP Advice 基础

Spring AOP Advice 基础

作者: Tinyspot | 来源:发表于2022-11-06 07:54 被阅读0次

1. Advice 增强

  • Advice 增强,定义拦截之后方法要做什么
  • 接口 org.aopalliance.aop.Advice
  • 5 种增强
    • MethodBeforAdvice 前置增强
    • AfterReturningAdvice 后置增强
    • MethodInterceptor 环绕增强
    • ThrowsAdvice 异常抛出增强
    • IntroductionInterceptor 引介增强,通过扩展DelegatingIntroductionInterceptor来实现引介增强
image.png

2. MethodInterceptor

  • 环绕通知拦截器

2.1 普通用法

public class BusinessInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("before...");
        // 通过反射调用目标方法
        Object obj = invocation.proceed();
        System.out.println("after...");
        return obj;
    }
}

2.2 Advisor

public class MyInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        // do something
        return invocation.proceed();
    }
}
@Configuration
public class BusinessInterceptorConfig {
    public static final String execution = "execution(* com.example.concrete.interceptor.*.*(..))";
    @Bean
    public Advisor defaultPointcutAdvisor() {
        MyInterceptor interceptor = new MyInterceptor();
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(execution);
        advisor.setPointcut(pointcut);
        advisor.setAdvice(interceptor);
        return advisor;
    }
}

相关文章

  • Spring AOP 代理

    Spring AOP 代理 1. Spring AOP 增强类型 AOP 联盟为通知 Advice 定义了 org...

  • Spring一般切面

    Spring一般切面 Spring 实现了AOP联盟Advice这个接口=>org.aopalliance.aop...

  • Sping - AOP - Advice

    原文地址:https://mkyong.com/spring/spring-aop-examples-advice...

  • Spring AOP

    Spring AOP 一、面向切面编程 1. AOP术语 1.1 ADVICE(增强) Spring提供了以下几种...

  • 03 AOP学习之五种通知

    Spring AOP五种通知详解 spring aop通知(advice)分成五类: 前置通知Before adv...

  • 笔试题 计算机

    spring aop通知(advice)分成五类:前置通知[Before advice]:在连接点前面执行,前置通...

  • Spring AOP五种通知及其执行顺序

    spring aop通知(advice)分成五类:Spring-AOP的5种通知 - 不违本心 - 博客园 前置通...

  • Spring AOP(五)切入点和通知

    本文主要描述 Spring AOP 中的 Pointcut 和 Advice 接口。 我们从 ProxyFacto...

  • spring AOP总结

    AOP术语解释 Advice (通知)切面的使用时机,spring aop有5种时机before(方法调用前)af...

  • Spring与设计模式

    适配器模式 在Spring的Aop中,使用的Advice(通知)来增强被代理类的功能。每个类型Advice都有对应...

网友评论

      本文标题:Spring AOP Advice 基础

      本文链接:https://www.haomeiwen.com/subject/mkwvtdtx.html