Spring的奥秘AOP(四)

简介: 前文讲了很多原理性的东西,使用起来比较繁琐,通过Spring框架提供的注解能很方便的实现切面功能。Spring中使用注解方式实现AOP,采用@AspectJ方式实现,首先确定需要切入的方法,也就是连接点

前文讲了很多原理性的东西,使用起来比较繁琐,通过Spring框架提供的注解能很方便的实现切面功能。


Spring中使用注解方式实现AOP,采用@AspectJ方式实现,首先确定需要切入的方法,也就是连接点


@Service
public class UserServiceMethod {
    public void add(String name) {
        System.out.println("UserServiceMethod add name is:" + name);
    }
}


开发切面


有了连接点,还需要切面通过切面描述AOP其他信息,来描述流程的织入


@Aspect
@Component
public class LogAgent {
    @Before("execution(* com.niu.dao.UserServiceMethod.add(..))")
    public void beforeAdd(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("before 方法规则拦截:" + method.getName());
    }
    @After("execution(* com.niu.dao.UserServiceMethod.add(..))")
    public void afterAdd(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("after 方法规则拦截:" + method.getName());
    }
    @AfterReturning("execution(* com.niu.dao.UserServiceMethod.add(..))")
    public void afterReturnAdd(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("afterReturn 方法规则拦截:" + method.getName());
    }
    @AfterThrowing("execution(* com.niu.dao.UserServiceMethod.add(..))")
    public void afterThrowsAdd(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("afterThrows 方法规则拦截:" + method.getName());
    }
}

其中注解中execution(* com.niu.dao.UserServiceMethod.add(..))是正则匹配,


execution表示正在执行的时候,拦截里面的正则匹配方法


*表示任意返回类型的方法


com.niu.dao.UserServiceMethod指定目标对象的全限定名称


add指定目标对象的方法


(..)表示任意参数进行匹配


AssertJ关于SpringAop切点的指示器:


  • args() 限定连接点方法参数
  • @args() 通过连接点方法参数上的注解
  • execution()用于匹配连接点上的执行方法
  • this()限制连接点匹配AOP代理bean引用为指定的类型
  • target目标对象
  • @target限制目标对象的配置
  • within限制连接点匹配指定的类型
  • @within() 限定连接点带有匹配注解类型
  • @annotation()限定带有指定注解的连接点


定义切点


在上面切面定义中,可以看到@before,@After等注解,还会定义一个正则,这个正则作用就是定义什么时候启用AOP,毕竟不是所有的功能都需要AOP,在上述代码中每个注解都有一个正则,这显得很冗余。为了解决这个问题,Spring定义了切点的概念(PointCut),切点的作用就是向Spring描述哪些类的哪些方法需要启动AOP编程。有了切点的概念,就可以吧代码写的更简洁:


@Aspect
@Component
public class LogAgent2 {
    @Pointcut("execution(* com.niu.dao.UserServiceMethod.add(..))")
    public void pointCut() {
        System.out.println("pointCut");
    }
    @Before("pointCut()")
    public void beforeAdd(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("before 方法规则拦截:" + method.getName());
    }
    @After("pointCut()")
    public void afterAdd(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("after 方法规则拦截:" + method.getName());
    }
    @AfterReturning("pointCut()")
    public void afterReturnAdd(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("after Return 方法规则拦截:" + method.getName());
    }
}

测试AOP,两种方式输出完全一致

@SpringBootApplication
public class App {
    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
        System.out.println("Start app success.");
        UserServiceMethod userServiceMethod = context.getBean(UserServiceMethod.class);
        userServiceMethod.add();
    }
}
//输出:
Start app success.
before 方法规则拦截:add
UserServiceMethod add 
after 方法规则拦截:add
after Return 方法规则拦截:add


引入


如果想检测用户信息是否为空,入参为空时则不打印,但是原接口中没有该校验,该怎么办呢? 这时应该引入Spring增强接口功能,为接口引入新的接口


首先提供校验接口:


public interface UserValidator {
    boolean validate(String name);
}
public class UserValidatorImpl implements UserValidator {
    @Override
    public boolean validate(String name) {
        System.out.println("引入校验");
        return StringUtils.isEmpty(name);
    }
}

引入新的接口:

@Aspect
@Component
public class MyAspect {
    @DeclareParents(value = "com.niu.dao.UserService+",
            defaultImpl = UserValidatorImpl.class)
    public UserValidator userValidator;
}

这里@DeclareParents作用是引入新的类来增强服务,必须有value和defaultImpl配置:

value:指向要增强功能的目标对象,配置为UserService defaultImpl:引入增强功能的类,这里配置为UserValidatorImpl


public static void main(String[] args) throws Exception {
    ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
    System.out.println("Start app success.");
    UserValidator userService = (UserValidator)context.getBean(UserService.class);
    if(userService.validate("steven")){
        System.out.println("Valid name");
    }
}
输出:Valid name

说明接口强转类型后,可以使用新方法,证明已有UserValidator的方法。


通知获取参数


在上述的通知中,大部分没有通知传递参数,可以通过使用连接点(Joinpoint)获取参数

@Before("pointCut() && args(name)")
public void beforeAdd(JoinPoint joinPoint, String name) {
    System.out.println(Arrays.toString(joinPoint.getArgs()));;
    System.out.println(name);;
}
输出:
[steven]
steven
复制代码

正则式pointCut() && args(name)中pointCut()表示原来定义的切点规则,并且约定将连接点(目标对象方法)名称为name的参数传递进来,通过连接点getArgs可以获取到所有参数,对于连接点参数还可以获取到目标对象的信息,从而完成需要的切面任务,下面是不用入参的形式:

@Before("pointCut()")
public void beforeAdd(JoinPoint joinPoint) {
    System.out.println(Arrays.toString(joinPoint.getArgs()));;
}
输出:
[steven]


织入


织入是一个生成动态代理对象并将切面的目标对象方法编织为约定的流程,一般都是采用接口加实现类的模式,这也是Spring推荐的方式,前问介绍过动态代理有很多实现方式JDK、CGLIB等,查资料说在Spring中如果要实现AOP的类有接口则用JDK动态代理执行,如果没有接口则用CGLib运行


跟下断点验证一下,我试过两种方式,不管实现类有没有接口都是使用的CGlib,不知道是不是网上解释有误还是SpringBoot2采用方式变更


image.png


多个切面


Spring支持多个切面运行

提供三个切面

@Aspect
@Component
public class LogAgent {
    @Before("execution(* com.niu.dao.UserServiceMethod.add(..))")
    public void beforeAdd(JoinPoint joinPoint) {
        System.out.println("LogAgent 拦截");
    }
}
@Aspect
@Component
public class LogAgent2 {
    @Pointcut("execution(* com.niu.dao.UsrTestService.add(..))")
    public void pointCut() {
        System.out.println("pointCut");
    }
    @Before("pointCut()")
    public void beforeAdd(JoinPoint joinPoint) {
        System.out.println("LogAgent2 拦截");;
    }
}
@Aspect
@Component
public class LogAgent3 {
    @Pointcut("execution(* com.niu.dao.UsrTestService.add(..))")
    public void pointCut() {
        System.out.println("pointCut");
    }
    @Before("pointCut()")
    public void beforeAdd(JoinPoint joinPoint) {
        System.out.println("LogAgent3 拦截");;
    }
}
输出:
LogAgent3 拦截
LogAgent 拦截
LogAgent2 拦截

结果每次输出拦截的顺序是不固定的,如果需要固定的执行的顺序可以在类上加Order注解,比如:

@Aspect
@Component
@Order(1)
public class LogAgent2 {
    @Pointcut("execution(* com.niu.dao.UsrTestService.add(..))")
    public void pointCut() {
        System.out.println("pointCut");
    }
}
输出:
LogAgent2 拦截
LogAgent3 拦截
LogAgent 拦截

可以根据需要排序,如果再加上after方法的打印,可以看到这是一个典型的职责链模型的顺序,有兴趣的可以自己在研究

LogAgent2 拦截
LogAgent3 拦截
LogAgent 拦截
UserServiceMethod add name is:steven
LogAgent1 :after 方法规则拦截:add
LogAgent3:after 方法规则拦截:add
LogAgent2:after 方法规则拦截:add


结语


关于AOP的文章就写到这了,功能其实并不难,重要的思想,平常开发中也可多考虑这种解耦方式。关于这两种实现的效率问题找了一些资料:在1.6和1.7的时候,JDK动态代理的速度要比CGLib动态代理的速度要慢,但是并没有教科书上的10倍差距,在JDK1.8的时候,JDK动态代理的速度已经比CGLib动态代理的速度快,这块有兴趣的可以再研究下,目前效率应该是差不多的。


目录
相关文章
|
XML 安全 Java
使用 Spring 的 @Aspect 和 @Pointcut 注解简化面向方面的编程 (AOP)
面向方面编程(AOP)通过分离横切关注点,如日志、安全和事务,提升代码模块化与可维护性。Spring 提供了对 AOP 的强大支持,核心注解 `@Aspect` 和 `@Pointcut` 使得定义切面与切入点变得简洁直观。`@Aspect` 标记切面类,集中处理通用逻辑;`@Pointcut` 则通过表达式定义通知的应用位置,提高代码可读性与复用性。二者结合,使开发者能清晰划分业务逻辑与辅助功能,简化维护并提升系统灵活性。Spring AOP 借助代理机制实现运行时织入,与 Spring 容器无缝集成,支持依赖注入与声明式配置,是构建清晰、高内聚应用的理想选择。
946 0
|
监控 安全 Java
Spring AOP实现原理
本内容主要介绍了Spring AOP的核心概念、实现机制及代理生成流程。涵盖切面(Aspect)、连接点(Join Point)、通知(Advice)、切点(Pointcut)等关键概念,解析了JDK动态代理与CGLIB代理的原理及对比,并深入探讨了通知执行链路和责任链模式的应用。同时,详细分析了AspectJ注解驱动的AOP解析过程,包括切面识别、切点表达式匹配及通知适配为Advice的机制,帮助理解Spring AOP的工作原理与实现细节。
1785 13
|
XML Java 开发者
Spring Boot中的AOP实现
Spring AOP(面向切面编程)允许开发者在不修改原有业务逻辑的情况下增强功能,基于代理模式拦截和增强方法调用。Spring Boot通过集成Spring AOP和AspectJ简化了AOP的使用,只需添加依赖并定义切面类。关键概念包括切面、通知和切点。切面类使用`@Aspect`和`@Component`注解标注,通知定义切面行为,切点定义应用位置。Spring Boot自动检测并创建代理对象,支持JDK动态代理和CGLIB代理。通过源码分析可深入了解其实现细节,优化应用功能。
845 6
|
11月前
|
XML Java 数据格式
《深入理解Spring》:AOP面向切面编程深度解析
Spring AOP通过代理模式实现面向切面编程,将日志、事务等横切关注点与业务逻辑分离。支持注解、XML和编程式配置,提供五种通知类型及丰富切点表达式,助力构建高内聚、低耦合的可维护系统。
|
人工智能 监控 安全
如何快速上手【Spring AOP】?核心应用实战(上篇)
哈喽大家好吖~欢迎来到Spring AOP系列教程的上篇 - 应用篇。在本篇,我们将专注于Spring AOP的实际应用,通过具体的代码示例和场景分析,帮助大家掌握AOP的使用方法和技巧。而在后续的下篇中,我们将深入探讨Spring AOP的实现原理和底层机制。 AOP(Aspect-Oriented Programming,面向切面编程)是Spring框架中的核心特性之一,它能够帮助我们解决横切关注点(如日志记录、性能统计、安全控制、事务管理等)的问题,提高代码的模块化程度和复用性。
|
设计模式 Java 开发者
如何快速上手【Spring AOP】?从动态代理到源码剖析(下篇)
Spring AOP的实现本质上依赖于代理模式这一经典设计模式。代理模式通过引入代理对象作为目标对象的中间层,实现了对目标对象访问的控制与增强,其核心价值在于解耦核心业务逻辑与横切关注点。在框架设计中,这种模式广泛用于实现功能扩展(如远程调用、延迟加载)、行为拦截(如权限校验、异常处理)等场景,为系统提供了更高的灵活性和可维护性。
1567 0
|
XML Java 测试技术
Spring AOP—通知类型 和 切入点表达式 万字详解(通俗易懂)
Spring 第五节 AOP——切入点表达式 万字详解!
1335 25
|
XML 安全 Java
Spring AOP—深入动态代理 万字详解(通俗易懂)
Spring 第四节 AOP——动态代理 万字详解!
691 24
|
XML Java 数据安全/隐私保护
Spring Aop该如何使用
本文介绍了AOP(面向切面编程)的基本概念和术语,并通过具体业务场景演示了如何在Spring框架中使用Spring AOP。文章详细解释了切面、连接点、通知、切点等关键术语,并提供了完整的示例代码,帮助读者轻松理解和应用Spring AOP。
673 2
Spring Aop该如何使用