Spring AOP注解案例及基本原理详解

所属分类: 软件编程 / java 阅读数: 38
收藏 0 赞 0 分享

切面:Aspect

切面=切入点+通知。在老的spring版本中通常用xml配置,现在通常是一个类带上@Aspect注解。切面负责将 横切逻辑(通知) 编织 到指定的连接点中。

目标对象:Target

将要被增强的对象。

连接点:JoinPoint

可以被拦截到的程序执行点,在spring中就是类中的方法。

切入点:PointCut

需要执行拦截的方法,也就是具体实施了横切逻辑的方法。切入点的规则在spring中通过AspectJ pointcut expression language来描述。

切入点与连接点的区别:连接点是所有可以被"切"的点;切入点是真正要切的点。

通知:Advice

针对切入点的横切逻辑,包含“around”、“before”和“after”等不同类型的通知。

通知的作用点如其命名:

  • before:在切入点之前执行
  • after:在切入点之后执行
  • around:在切入点拦截方法,自定义前后,更灵活

还有一些异常处理的通知,这里不一一举例

织入:Weaving

将切面和目标对象连接起来,创建代理对象的过程。spring中用的是动态代理。假如目标对象有接口,使用jdk动态代理;否则使用cglib动态代理。

说了这么多概念,看看代码实现可能会使读者理解的更深刻一些,这里简单写一个通过注解增强方法的AOP-Demo。
首先是切面类:

package com.example.demo.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author Fcb
 * @date 2020/6/20
 * @description 切面类=切入点+通知
 */
@Aspect
@Component
public class LogAspect {

  //这个方法定义了切入点
  @Pointcut("@annotation(com.example.demo.aop.anno.MyLog)")
  public void pointCut() {}

  //这个方法定义了具体的通知
  @After("pointCut()")
  public void recordRequestParam(JoinPoint joinPoint) {
    for (Object s : joinPoint.getArgs()) {
      //打印所有参数,实际中就是记录日志了
      System.out.println("after advice : " + s);
    }
  }

  //这个方法定义了具体的通知
  @Before("pointCut()")
  public void startRecord(JoinPoint joinPoint) {
    for (Object s : joinPoint.getArgs()) {
      //打印所有参数
      System.out.println("before advice : " + s);
    }
  }

  //这个方法定义了具体的通知
  @Around("pointCut()")
  public Object aroundRecord(ProceedingJoinPoint pjp) throws Throwable {
    for (Object s : pjp.getArgs()) {
      //打印所有参数
      System.out.println("around advice : " + s);
    }
    return pjp.proceed();
  }
}

注解:

package com.example.demo.aop.anno;
import java.lang.annotation.*;
/**
 * @author Fcb
 * @date 2020/6/20
 * @description
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface MyLog {
}

目标类:

package com.example.demo.aop.target;

import com.example.demo.aop.anno.MyLog;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Fcb
 * @date 2020/6/20
 * @description
 */
@RestController
public class MockController {

  @RequestMapping("/hello")
  @MyLog
  public String helloAop(@RequestParam String key) {
    System.out.println("do something...");
    return "hello world";
  }

}

最后是测试类:

package com.example.demo.aop.target;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
 * @author Fcb
 * @date 2020/6/20
 * @description
 */
@SpringBootTest
class MockControllerTest {
  @Autowired
  MockController mockController;

  @Test
  void helloAop() {
    mockController.helloAop("aop");
  }
}

控制台结果:

around advice : aop
before advice : aop
do something...
after advice : aop

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多