返回

透过棱镜看Spring AOP(二)

后端

开启AOP

基于AspectJ注解形式开启AOP需要使用@EnableAspectJAutoProxy注解,该注解可以自动代理所有被@Aspect注解的类。使用@Aspect注解的类称为切面(Aspect),它定义了横切关注点和相关的通知。

@SpringBootApplication
@EnableAspectJAutoProxy
public class AopApplication {

    public static void main(String[] args) {
        SpringApplication.run(AopApplication.class, args);
    }
}

定义切面

@Aspect
@Component
public class LoggingAspect {

    @Before("execution(* com.example.demo.service.*.*(..))")
    public void logBefore(JoinPoint joinPoint) {
        System.out.println("Before method: " + joinPoint.getSignature().getName());
    }
}

织入点

织入点(Joinpoint)是指程序执行过程中可以应用切面的特定点。Spring AOP支持多种织入点,包括方法调用、方法执行、字段访问和异常处理等。

@Before("execution(* com.example.demo.service.*.*(..))")

上面的代码中,织入点定义为“所有com.example.demo.service包下的所有方法的执行”。

通知

通知(Advice)是在织入点执行的代码。Spring AOP支持多种通知类型,包括前置通知(Before)、后置通知(After)、返回通知(AfterReturning)、异常通知(AfterThrowing)和环绕通知(Around)。

@Before("execution(* com.example.demo.service.*.*(..))")
public void logBefore(JoinPoint joinPoint) {
    System.out.println("Before method: " + joinPoint.getSignature().getName());
}

上面的代码中,通知类型为前置通知,它将在方法执行之前执行。

切点

切点(Pointcut)是定义通知应用范围的表达式。Spring AOP支持多种切点表达式,包括Ant风格表达式和AspectJ表达式。

@Before("execution(* com.example.demo.service.*.*(..))")

上面的代码中,切点表达式为“execution(* com.example.demo.service..(..))”,它匹配所有com.example.demo.service包下的所有方法的执行。

连接点

连接点(Joinpoint)是程序执行过程中的一个特定点,它可以被切面拦截。Spring AOP支持多种连接点类型,包括方法调用、方法执行、字段访问和异常处理等。

@Before("execution(* com.example.demo.service.*.*(..))")
public void logBefore(JoinPoint joinPoint) {
    System.out.println("Before method: " + joinPoint.getSignature().getName());
}

上面的代码中,连接点类型为方法调用。

切面

切面(Aspect)是一个模块,它定义了横切关注点和相关的通知。Spring AOP支持多种切面类型,包括AspectJ切面和注解驱动切面。

@Aspect
@Component
public class LoggingAspect {

    @Before("execution(* com.example.demo.service.*.*(..))")
    public void logBefore(JoinPoint joinPoint) {
        System.out.println("Before method: " + joinPoint.getSignature().getName());
    }
}

上面的代码中,LoggingAspect是一个切面,它定义了横切关注点“日志记录”和相关的通知“logBefore”。

增强

增强(Advice)是在织入点执行的代码。Spring AOP支持多种增强类型,包括前置增强、后置增强、返回增强、异常增强和环绕增强。

@Before("execution(* com.example.demo.service.*.*(..))")
public void logBefore(JoinPoint joinPoint) {
    System.out.println("Before method: " + joinPoint.getSignature().getName());
}

上面的代码中,logBefore是一个增强,它将在方法执行之前执行。

代理

代理(Proxy)是Spring AOP用来实现AOP的关键技术。Spring AOP使用JDK动态代理或CGLIB动态代理来创建代理对象,代理对象可以拦截方法调用并执行切面的通知。

@SpringBootApplication
@EnableAspectJAutoProxy
public class AopApplication {

    public static void main(String[] args) {
        SpringApplication.run(AopApplication.class, args);
    }
}

上面的代码中,@EnableAspectJAutoProxy注解开启了Spring AOP,Spring AOP将自动为被@Aspect注解的类创建代理对象。

Java

Java是一种面向对象编程语言,它具有强大的面向对象特性,如继承、多态和封装。Java广泛应用于各种领域,包括Web开发、移动开发、桌面应用开发和嵌入式系统开发。

@SpringBootApplication
@EnableAspectJAutoProxy
public class AopApplication {

    public static void main(String[] args) {
        SpringApplication.run(AopApplication.class, args);
    }
}

上面的代码是Java代码,它使用了Spring Boot和Spring AOP框架。

面向方面编程

面向方面编程(Aspect-Oriented Programming,AOP)是一种编程范式,它允许将横切关注点从应用程序的主流程中分离出来,从而提高代码的可维护性和可重用性。Spring AOP是Java平台上最流行的AOP框架之一。