spring AOP

AOP

         AOP(Aspect-Oriented Programming,面向切面编程)它提供了一种在应用程序中通过横向切割关注点的方法。

        在传统的面向对象编程中,我们通常把某个功能集中在某个类的某个方法中,并且java代码的执行顺序是从上至下(纵向)的,AOP则通过在纵向代码的特定位置植入额外的代码(横切关注点),实现面向切面编程。

spring AOP

        Spring AOP基于代理模式实现,它使用动态代理技术在运行时为目标对象创建一个代理对象,以拦截和处理与横切关注点相关的方法调用。

关键概念:

  • 切面(Aspect): 切面是横切关注点的模块化单元。它由切点和通知组成。切点定义了在何处应用通知,而通知则定义了在切点处执行的操作。

  • 切点(Pointcut): 切点是一组匹配连接点(Join Point)的规则。连接点是应用程序执行的特定位置,如方法调用、方法执行、异常抛出等。切点表达式可以通过使用类名、方法名、注解等条件进行匹配。

  • 通知(Advice): 通知是在切点处执行的具体操作。在Spring AOP中,有以下几种类型的通知:

    • 前置通知(Before Advice):在目标方法执行之前执行。
    • 后置通知(After Advice):在目标方法执行之后执行,不论方法是否发生异常。
    • 返回通知(After Returning Advice):在目标方法成功执行并返回结果之后执行。
    • 异常通知(After Throwing Advice):在目标方法抛出异常后执行。
    • 环绕通知(Around Advice):在目标方法执行之前和之后都执行。
  • 引入(Introduction): 引入允许向现有的类添加新的方法或属性。它允许在不修改现有代码的情况下向现有类添加新功能。

  • 织入(Weaving): 织入是将切面应用到目标对象并创建代理对象的过程。织入可以在编译时、类加载时或运行时进行。Spring AOP采用运行时织入。 

作用:

  • 将横切关注点与业务代码隔离,提高横切点的扩展性和可读性。
  • 避免重复代码,提高横切点的复用性。

springboot样例 

1、创建切面,使用@Aspect注解标记它为切面,并使用@Component注解将其作为Spring的Bean。切面中定义了两个通知方法:beforeMethodExecutionafterMethodExecutionbeforeMethodExecution方法在切点(execution(* com.example.demo.service.*.*(..)))匹配的方法执行之前执行,输出方法名。afterMethodExecution方法在切点匹配的方法成功执行并返回结果后执行,输出方法名和结果。

@Aspect
@Component
public class LoggingAspect {

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

    @AfterReturning(pointcut = "execution(* com.example.demo.service.*.*(..))", returning = "result")
    public void afterMethodExecution(JoinPoint joinPoint, Object result) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("After executing method: " + methodName + ". Result: " + result);
    }
}

2、创建服务类

@Service
public class MyService {

    public void doSomething() {
        System.out.println("Doing something...");
    }
}

3、测试

@SpringBootApplication
public class MyApp {
    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(MyApp.class, args);

        MyService myService = context.getBean(MyService.class);
        myService.doSomething();
    }
}

输出如下

Before executing method: doSomething
Doing something...
After executing method: doSomething. Result: null

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值