spring aop思想和spring boot aop配置

迈不过友情╰ 2022-02-27 02:12 545阅读 0赞

其实就这点功能:https://www.yiibai.com/spring/spring-aop-aspectj-in-xml-configuration-example.html#article-start

  1. //限定匹配带有指定注解的方法的链接点
  2. @Pointcut("@annotation(com.ruoyi.common.datascope.annotation.DataScope)")

一、AOP(面向切面编程)

轻松理解AOP思想(面向切面编程) https://www.cnblogs.com/Wolfmanlq/p/6036019.html

需要从不同的角度来看待同一个事物。这里的“方面”,指的是事物的外在特性在不同观察角度下的体现。而在AOP中,Aspect的含义,可能更多的理解为“切面”比较合适。可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术。AOP实际是GoF设计模式的延续,设计模式孜孜不倦追求的是调用者和被调用者之间的解耦,提高代码的灵活性和可扩展性,AOP可以说也是这种目标的一种实现。

1.1:静态代理(只看静态代理,不要往下看): https://www.cnblogs.com/lcngu/p/5339555.html

1.2:动态代理

二、OOP(面向对象编程)

三、Spring

Spring是一个库,它的功能是提供了一个软件框架,这个框架目的是使软件之间的逻辑更加清晰,配置更灵活,实现这个目的的手段使用AOP和IoC,而AOP和IoC是一种思想

四、AOP配置

  1. @Aspect 将一个java类定义为切面类
  2. @Pointcut 定义一个切入点(可以是一个正则表达式,例如某个package下的所有函数,也可以是一个注解)
  3. 根据需要在切入点的不同位置切入内容:
  4. @Before 在切入点开始时,切入内容
  5. @After 在切入点结尾处,切入内容
  6. @Around 在切入点前后处,切入内容,并自己控制何时执行内容的切入
  7. @AfterReturning 在切入点return内容之后切入内容,用来定义对返回值的处理逻辑
  8. @AfterThrowing 在切入点抛出异常后切入内容,用来定义抛出异常之后的处理逻辑

4.1:spring xml版

代码地址:https://download.csdn.net/download/qq_16946803/11061438

4.2:spring 注解版

代码地址:https://download.csdn.net/download/qq_16946803/11061475

4.3:spring boot 注解版

注:4.3.1:切入点表达式 https://www.cnblogs.com/duenboa/p/6665474.html

4.3.2:spring boot aop讲解 https://www.jianshu.com/p/939b965652cb

  1. <!--springboot aop 包-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-aop</artifactId>
  5. </dependency>
  6. package com.example.demo.config;
  7. import org.aspectj.lang.annotation.After;
  8. import org.aspectj.lang.annotation.AfterReturning;
  9. import org.aspectj.lang.annotation.AfterThrowing;
  10. import org.aspectj.lang.annotation.Aspect;
  11. import org.aspectj.lang.annotation.Before;
  12. import org.aspectj.lang.annotation.Pointcut;
  13. import org.springframework.context.annotation.Configuration;
  14. @Aspect
  15. @Configuration
  16. public class AopConfig {
  17. /**
  18. * 切入点配置1111111
  19. */
  20. @Pointcut("execution(* com.example.demo.service..*(..))") //需要切入的包或类名
  21. public void txPointCut(){//txPointCut()
  22. }
  23. @Before("txPointCut()")
  24. public void begin(){
  25. System.out.println("开启事务");
  26. }
  27. @AfterReturning("txPointCut()")
  28. public void commit(){
  29. System.out.println("提交事务");
  30. }
  31. /*public void rollback(){
  32. System.out.println("回滚事务");
  33. }*/
  34. @AfterThrowing(value="txPointCut()",throwing="ex")
  35. public void rollback(Throwable ex){
  36. System.out.println("回滚事务"+ex.getMessage());
  37. }
  38. public void close(){
  39. System.out.println("释放资源");
  40. }
  41. /**
  42. * 切入点配置222222
  43. */
  44. @Pointcut("execution(* com.example.demo.controller..*(..))") //需要切入的包或类名
  45. public void txPointCut(){//txPointCut()
  46. }
  47. }

发表评论

表情:
评论列表 (有 0 条评论,545人围观)

还没有评论,来说两句吧...

相关阅读

    相关 spring boot aop

    AOP一些理论和spring boot环境搭建免去,直接aop实现(LZ也是自己跟着官网学spring boot,原理概念性的东西,也是看官网和别人的分享,这里不敢说多了,会出