AOP简介

AOP(面向切面编程)是一种编程范式,Spring AOP是基于代理模式的AOP框架,它通过动态代理实现切面的织入,更加轻量级和易于使用。

  • Joinpoint (连接点):类里面可以被增强的方法即为连接点。例如,想修改哪个方法的功能,那么该方法就是一个连接点。
  • Pointcut(切入点):对Joinpoint进行拦截的定义即为切入点。例如,拦截所有以insert 开始的方法,这个定义即为切入点。
  • Advice (通知):拦截到Joinpoint 之后所要做的事情就是通知。例如,上文说到的打印日志监控。通知分为前置通知、后置通知、异常通知、最终通知和环绕通知。
  • Aspect ( 切面): Pointcut 和Advice的结合。
  • Target (目标对象):要增强的类称为Target。

引入Maven依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

动态数据源配置

@Component
@Aspect
public class ExampleAspect {
    @Pointcut("execution(* com.example.*.*(..))")
    public void foo() {
    }

    @Before(value = "foo()")
    public void before(JoinPoint jp) {
        String name = jp.getSignature().getName();
        System.out.println(name + "方法开始执行...");
    }

    @After(value = "foo()")
    public void after(JoinPoint jp) {
        String name = jp.getSignature().getName();
        System.out.println(name + "方法执行结束...");
    }

    @AfterReturning(value = "foo()", returning = "result")
    public void afterReturning(JoinPoint jp, Object result) {
        String name = jp.getSignature().getName();
        System.out.println(name + "方法返回值为: " + result);
    }

    @AfterThrowing(value = "foo ()", throwing = "e")
    public void afterThrowing(JoinPoint jp, Exception e) {
        String name = jp.getSignature().getName();
        System.out.println(name + "方法抛异常了,异常是: " + e.getMessage());
    }

    @Around("foo()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        return pjp.proceed();
    }
}

@Aspect注解表明这是一个切面类。

  • @Pointcut注解:这是一个切入点定义。execution 中的第一个 * 表示方法返回任意值,第二个 * 表示service 包下的任意类,第三个 * 表示类中的任意方法,括号中的两个点表示方法参数任意,即这里描述的切入点为service 包下所有类中的所有方法。
    • 常用的@Pointcut值包括:

      • execution:用于匹配方法执行的连接点。可以使用通配符和正则表达式来指定匹配的方法。
      • within:用于匹配指定类型内的方法执行连接点。
      • this:用于匹配当前AOP代理对象类型的连接点。
      • target:用于匹配目标对象类型的连接点。
      • args:用于匹配方法参数类型的连接点。
      • @annotation:用于匹配使用指定注解修饰的方法连接点。
      • bean:用于匹配指定名称的Bean对象的连接点。
      • 这些@Pointcut值可以与逻辑运算符(&&、||、!)一起使用,以实现更复杂的切点表达式。例如:
        @Pointcut("execution(* com.example.service..(..)) && !execution(* com.example.service.internal..(..))")
        上述切点表达式匹配了com.example.service包下的所有方法,但排除了com.example.service.internal包下的方法。
        请注意,上述只是常见的@Pointcut值示例,实际使用时可以根据需要进行更灵活的配置和组合。
    • @Pointcut常见表达式包括:

      • execution:用于匹配方法执行的连接点。例如:
        execution(public * com.example.service..(..)):匹配com.example.service包下所有public方法的执行。
        execution(* com.example.service.UserService.*(..)):匹配com.example.service.UserService类中的所有方法的执行。
      • within:用于匹配指定类型内的方法执行连接点。例如:
        within(com.example.service.*):匹配com.example.service包下所有类的方法执行。
        within(com.example.service.UserService):匹配com.example.service.UserService类中的所有方法的执行。
      • this:用于匹配当前AOP代理对象类型的连接点。例如:
        this(com.example.service.UserService):匹配当前AOP代理对象类型为com.example.service.UserService的方法执行。
      • target:用于匹配目标对象类型的连接点。例如:
        target(com.example.service.UserService):匹配目标对象类型为com.example.service.UserService的方法执行。
      • args:用于匹配方法参数类型的连接点。例如:
        args(java.lang.String):匹配方法参数类型为java.lang.String的方法执行。
      • @annotation:用于匹配使用指定注解修饰的方法连接点。例如:
        @annotation(org.springframework.transaction.annotation.Transactional):匹配使用org.springframework.transaction.annotation.Transactional注解修饰的方法执行。
      • bean:用于匹配指定名称的Bean对象的连接点。例如:
        bean(userService):匹配名称为userService的Bean对象的方法执行。
  • @Before注解:表示这是一个前置通知,该方法在目标方法执行之前执行。通过JoinPoint参数可以获取目标方法的方法名、修饰符等信息。
  • @After注解:表示这是一个后置通知,该方法在目标方法执行之后执行。
  • @AfterReturning注解:表示这是一个返回通知,在该方法中可以获取目标方法的返回值。@AfterReturmning 注解的returning参数是指返回值的变量名,对应方法的参数。注意,在方法参数中定义了result 的类型为Object,表示目标方法的返回值可以是任意类型,若result 参数的类型为Long,则该方法只能处理目标方法返回值为Long的情况。
  • @AfterThrowing注解:表示这是一个异常通知,即当目标方法发生异常时,该方法会被调用,异常类型为Exception 表示所有的异常都会进入该方法中执行,若异常类型为ArithmeticException,则表示只有目标方法抛出的ArithmeticException异常才会进入该方法中处理。
  • @Around注解:表示这是一一个环绕通知。环绕通知是所有通知里功能最为强大的通知,可以实现前置通知、后置通知、异常通知以及返回通知的功能。目标方法进入环绕通知后,通过调用ProceedingJoinPoint对象的proceed方法使目标方法继续执行,开发者可以在此修改目标方法的执行参数、返回值等,并且可以在此处理目标方法的异常。
    --

本文到此结束,感谢您的观看!!!

posted on 2023-07-31 21:09  超然楼  阅读(14)  评论(0编辑  收藏  举报