什么是通知呢?有哪些类型呢?

1 个回答

  • 冰儿
    我疯狂地奔跑,只想为你承担一切的伤害,我竖起了山丘,不愿你看见我丑陋的脸庞。
    ### 什么是通知? 在Spring AOP中,通知(Advice)是指在方法执行前或执行后要执行的动作。通知是程序执行时通过Spring AOP框架触发的代码段,用于在切入点(Join Point)处插入额外的行为。 ### 通知的类型 Spring AOP提供了五种类型的通知: 1. **Before(前置通知)**: - **描述**:在方法执行前调用的通知。 - **示例**: ```java @Before("execution(* com.example.service.*.*(..))") public void beforeAdvice() { System.out.println("Before method execution"); } ``` 2. **After(后置通知)**: - **描述**:在方法执行之后调用的通知,无论方法执行是否成功。 - **示例**: ```java @After("execution(* com.example.service.*.*(..))") public void afterAdvice() { System.out.println("After method execution"); } ``` 3. **AfterReturning(返回通知)**: - **描述**:仅当方法成功完成后执行的通知。 - **示例**: ```java @AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result") public void afterReturningAdvice(Object result) { System.out.println("After method returns: " + result); } ``` 4. **AfterThrowing(异常通知)**: - **描述**:在方法抛出异常退出时执行的通知。 - **示例**: ```java @AfterThrowing(pointcut = "execution(* com.example.service.*.*(..))", throwing = "error") public void afterThrowingAdvice(Throwable error) { System.out.println("After method throws exception: " + error); } ``` 5. **Around(环绕通知)**: - **描述**:在方法执行之前和之后调用的通知。 - **示例**: ```java @Around("execution(* com.example.service.*.*(..))") public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("Before method execution"); Object result = joinPoint.proceed(); System.out.println("After method execution"); return result; } ``` ### 示例代码 以下是一个完整的示例,展示了如何在Spring AOP中使用不同类型的通知: #### 切面类 ```java import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void beforeAdvice() { System.out.println("Before method execution"); } @After("execution(* com.example.service.*.*(..))") public void afterAdvice() { System.out.println("After method execution"); } @AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result") public void afterReturningAdvice(Object result) { System.out.println("After method returns: " + result); } @AfterThrowing(pointcut = "execution(* com.example.service.*.*(..))", throwing = "error") public void afterThrowingAdvice(Throwable error) { System.out.println("After method throws exception: " + error); } @Around("execution(* com.example.service.*.*(..))") public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("Before method execution"); Object result = joinPoint.proceed(); System.out.println("After method execution"); return result; } } ``` ### 配置文件(spring-aop.xml) ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.example"/> <aop:aspectj-autoproxy/> </beans> ``` ### 总结 - **Before**:在方法执行前调用。 - **After**:在方法执行后调用,无论方法执行是否成功。 - **AfterReturning**:在方法成功返回后调用。 - **AfterThrowing**:在方法抛出异常时调用。 - **Around**:在方法执行前后都调用。 通过使用这些通知类型,Spring AOP能够在方法执行的不同阶段插入额外的行为,从而实现灵活的面向切面编程(AOP)。
    发布于 2024-07-24 19:53 •IP属地 上海
    举报
    评论
    收藏