【Spring】面向切面之AOP

简介: 前面已经讲解了bean的装配技术,接着学习Spring中另外一个核心概念:切面

前言


前面已经讲解了bean的装配技术,接着学习Spring中另外一个核心概念:切面。


面向切面



面向切面编程

切面能够帮助模块化横切关注点,横切关注点可以被描述为影响应用的功能,如为业务添加安全和事务管理等。


AOP(Aspect Orient Programming)

  • 通知,通知定义切面何时被使用,Spring切面可以应用5种类型的通知。
  • 前置通知(Before),在目标方法被调用之前调用通知功能。
  • 后置通知(After),在目标方法完成之后调用通知,并不关心方法的输出。
  • 返回通知(AfterReturning),在目标方法成功执行之后调用通知。
  • 异常通知(AfterThrowing),在目标方法抛出异常后调用通知。
  • 环形通知(Around),通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为。
  • 连接点,在应用执行过程中能够插入切面的一个点。
  • 切点,匹配通知所要织入的一个或多个连接点。
  • 切面,通知和切点的结合。
  • 引入,允许向现有类添加新方法或属性。
  • 织入,把切面应用到目标对象并创建新的代理对象的过程,切面可以在指定的连接点被织入到目标对象中,在目标对象的生命周期中有多个点可以进行织入。
  • 编译期,在目标类编译时被织入,需要特殊的编译器支持。
  • 类加载器,切面在目标类加载到JVM时被织入,需要特殊类加载器。
  • 运行期,在应用运行的某个时刻被织入,AOP容器会为目标对象动态创建代理对象,这也是Spring AOP的织入方式。


Spring AOP

Spring对AOP的支持在很多方面借鉴了AspectJ项目,提供如下四种支持。

  • 基于代理的经典Spring AOP。
  • 纯POJO切面。
  • @AspectJ注解的切面。
  • 注入式AspectJ切面(适用于Spring各版本)。

Spring AOP构建在动态代理基础上,只能局限于对方法拦截;Spring在运行时通知对象(通过在代理类中包裹切面,在运行期把切面织入到Spring管理的bean中,代理类封装了目标类,并拦截被通知方法的调用,执行切面逻辑,再把调用转发给真正的目标bean);Spring只支持方法级别的连接点(基于动态代理决定)。


通过切点选择连接点


编写切点

首先先定义一个方法

package ch4
public interface Performance {
  void perform();
}

然后使用切点表达式设置当perform方法执行时触发通知的调用execution(* ch4.Performance.perform(..)),*表示并不关心返回值,然后指定具体的方法名,方法中的..表示切点要选择任意的perform方法。还可使用&&、and、||、or对切点进行限定。


切点中选择bean

切点表达式中可使用bean的ID来标识bean,如下切点表达式execution(* ch4.Performance.perform(..)) && bean(musicPerformance),表示限定beanID为musicPerformance时调用通知,其中musicPerformance是Performance的一个子类实现。


使用注解创建切面


定义切面

定义一个切面如下。

package ch4;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class Audience {
    @Before("execution(* ch4.Performance.perform(..))")
    public void silenceCellPhones() {
        System.out.println("Silencing cell phones");
    }
    @Before("execution(* ch4.Performance.perform(..))")
    public void takeSeats() {
        System.out.println("Taking seats");
    }
    @AfterReturning("execution(* ch4.Performance.perform(..))")
    public void applause() {
        System.out.println("CLAP CLAP CLAP CLAP");
    }
    @AfterThrowing("execution(* ch4.Performance.perform(..))")
    public void demandRefund() {
        System.out.println("Demanding a refund");
  }
}

可以看到配合注解和切点表达式可以使得在执行perform方法之前、之后完成指定动作,当然,对于每个方法都使用了execution切点表达式,可以进一步进行精简。

package ch4;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class Audience {
    @Pointcut("execution(* ch4.Performance.perform(..))")
    public void performance() {
    }
    @Before("performance()")
    public void silenceCellPhones() {
        System.out.println("Silencing cell phones");
    }
    @Before("performance()")
    public void takeSeats() {
        System.out.println("Taking seats");
    }
    @AfterReturning("performance()")
    public void applause() {
        System.out.println("CLAP CLAP CLAP CLAP");
    }
    @AfterThrowing("performance()")
    public void demandRefund() {
        System.out.println("Demanding a refund");
    }
}

可以看到使用@Pointcut定义切点,然后在其他方法中直接使用注解和切点方法即可,不需要再繁琐的使用execution切点表达式。


启动代理功能

在定义了注解后,需要启动,否则无法识别,启动方法分为在JavaConfig中显式配置和XML注解。

  • JavaConfig显式配置
package ch4;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class ConcertConfig {
    @Bean
    public Audience audience() {
        return new Audience();
    }
}
  • XML配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    <aop:aspectj-autoproxy/>
    <context:component-scan base-package="ch4"/>
</beans>


创建环绕通知

将被通知的目标方法完全包装起来,就像在一个通知方法中同时编写前置通知和后置通知。

package ch4;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class Audience {
    @Pointcut("execution(* ch4.Performance.perform(..)) && ! bean(musicPerformance)")
    public void performance() {
    }
    @Around("performance()")
    public void watchPerformance(ProceedingJoinPoint jp) {
        try {
            System.out.println("Silencing cell phones");
            System.out.println("Taking seats");
            jp.proceed();
            System.out.println("CLAP CLAP CLAP CLAP");
        } catch (Throwable e) {
            System.out.println("Demanding a refund");
        }
    }
}

使用Around注解表示环绕通知,注意需要调用proceed()方法来调用实际的通知方法。


处理通知中的参数

在perform方法中添加int number参数表示有多少观众,使用如下切点表达式execution(\* ch4.Performance.perform(int)) && args(number),表示需要匹配perform(int)型方法并且通知方法的参数名为number。

  • MusicPerformance如下
package ch4;
import org.springframework.stereotype.Service;
@Service
public class MusicPerformance implements Performance {
    public void perform(int number) {
        System.out.println("perform music, and the audience number is " + number);
    }
}
  • Audience如下
package ch4;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class Audience {
    @Pointcut("execution(* ch4.Performance.perform(int)) && args(number)")
    public void performance(int number) {
    }
    @Before("performance(int)")
    public void silenceCellPhones() {
        System.out.println("Silencing cell phones");
    }
    @Before("performance(int)")
    public void takeSeats() {
        System.out.println("Taking seats");
    }
    @AfterReturning("performance(int)")
    public void applause() {
        System.out.println("CLAP CLAP CLAP CLAP");
    }
    @AfterThrowing("performance(int)")
    public void demandRefund() {
        System.out.println("Demanding a refund");
    }
    @Around("performance(int)")
    public void watchPerformance(ProceedingJoinPoint jp) {
        try {
            System.out.println("Silencing cell phones");
            System.out.println("Taking seats");
            jp.proceed();
            System.out.println("CLAP CLAP CLAP CLAP");
        } catch (Throwable e) {
            System.out.println("Demanding a refund");
        }
    }
}
  • 测试AOPTest如下
package ch4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:spring-learning.xml")
public class AOPTest {
    @Autowired
    private Performance performance;
    @Test
    public void notNull() {
        assertNotNull(performance);
        performance.perform(100);
        System.out.println("++++++++++++++++++");
        performance.perform(999);
        System.out.println("++++++++++++++++++");
    }
}

运行结果:

Silencing cell phones

Taking seats

Taking seats

Silencing cell phones

perform music, and the audience number is 100

CLAP CLAP CLAP CLAP

CLAP CLAP CLAP CLAP

++++++++++++++++++

Silencing cell phones

Taking seats

Taking seats

Silencing cell phones

perform music, and the audience number is 999

CLAP CLAP CLAP CLAP

CLAP CLAP CLAP CLAP

++++++++++++++++++


在XML中声明切面

除了使用注解方式声明切面外,还可通过XML方式声明切面。


前置通知和后置通知

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    <aop:aspectj-autoproxy/>
    <context:component-scan base-package="ch4"/>
    <aop:config>
        <aop:aspect ref="audience">
            <aop:before
                pointcut="execution(* ch4.Performance.perform(..))"
                method="silenceCellPhones" />
            <aop:before
                    pointcut="execution(* ch4.Performance.perform(..))"
                    method="takeSeats" />
            <aop:after-returning
                    pointcut="execution(* ch4.Performance.perform(..))"
                    method="applause" />
            <aop:after-throwing
                    pointcut="execution(* ch4.Performance.perform(..))"
                    method="demandRefund" />
        </aop:aspect>
    </aop:config>
</beans>

将Audience注解删除后运行单元测试可得出正确结果;当然上述XML也有点复杂,可进一步简化。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    <aop:aspectj-autoproxy/>
    <context:component-scan base-package="ch4"/>
    <aop:config>
        <aop:aspect ref="audience">
            <aop:pointcut
                    id="performance"
                    expression="execution(* ch4.Performance.perform(..))" />
            <aop:before
                    pointcut-ref="performance"
                    method="silenceCellPhones" />
            <aop:before
                    pointcut-ref="performance"
                    method="takeSeats" />
            <aop:after-returning
                    pointcut-ref="performance"
                    method="applause" />
            <aop:after-throwing
                    pointcut-ref="performance"
                    method="demandRefund" />
        </aop:aspect>
    </aop:config>
</beans>


声明环绕通知

XML如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    <aop:aspectj-autoproxy/>
    <context:component-scan base-package="ch4"/>
    <aop:config>
        <aop:aspect ref="audience">
            <aop:pointcut
                    id="performance"
                    expression="execution(* ch4.Performance.perform(..))"/>
            <aop:around
                pointcut-ref="performance"
                method="watchPerformance" />
        </aop:aspect>
    </aop:config>
</beans>

运行单元测试,可得正确结果。


为通知传递参数

XML文件如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    <aop:aspectj-autoproxy/>
    <context:component-scan base-package="ch4"/>
    <aop:config>
        <aop:aspect ref="audience">
            <aop:pointcut
                    id="performance"
                    expression="execution(* ch4.Performance.perform(int)) and args(int)" />
            <aop:before
                pointcut-ref="performance"
                method="silenceCellPhones" />
            <aop:before
                    pointcut-ref="performance"
                    method="takeSeats" />
            <aop:after-returning
                    pointcut-ref="performance"
                    method="applause" />
            <aop:after-throwing
                    pointcut-ref="performance"
                    method="demandRefund" />
        </aop:aspect>
    </aop:config>
</beans>

运行单元测试,可得正确结果。


总结


AOP是Spring的核心概念,通过AOP,我们可以把切面插入到方法执行的周围,通过本篇博文可以大致了解AOP的使用方法。源码已经上传至github,欢迎fork and star。

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