spring aop思想和spring boot aop配置
其实就这点功能:https://www.yiibai.com/spring/spring-aop-aspectj-in-xml-configuration-example.html#article-start
//限定匹配带有指定注解的方法的链接点
@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配置
@Aspect 将一个java类定义为切面类
@Pointcut 定义一个切入点(可以是一个正则表达式,例如某个package下的所有函数,也可以是一个注解)
根据需要在切入点的不同位置切入内容:
@Before 在切入点开始时,切入内容
@After 在切入点结尾处,切入内容
@Around 在切入点前后处,切入内容,并自己控制何时执行内容的切入
@AfterReturning 在切入点return内容之后切入内容,用来定义对返回值的处理逻辑
@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
<!--springboot aop 包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
package com.example.demo.config;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;
@Aspect
@Configuration
public class AopConfig {
/**
* 切入点配置1111111
*/
@Pointcut("execution(* com.example.demo.service..*(..))") //需要切入的包或类名
public void txPointCut(){//txPointCut()
}
@Before("txPointCut()")
public void begin(){
System.out.println("开启事务");
}
@AfterReturning("txPointCut()")
public void commit(){
System.out.println("提交事务");
}
/*public void rollback(){
System.out.println("回滚事务");
}*/
@AfterThrowing(value="txPointCut()",throwing="ex")
public void rollback(Throwable ex){
System.out.println("回滚事务"+ex.getMessage());
}
public void close(){
System.out.println("释放资源");
}
/**
* 切入点配置222222
*/
@Pointcut("execution(* com.example.demo.controller..*(..))") //需要切入的包或类名
public void txPointCut(){//txPointCut()
}
}
还没有评论,来说两句吧...