Spring中环绕增强

今天带来一个简单的Spring中的环绕AOP切面的具体使用以及实现
直接废话不多说上代码!也相当于给自己做个笔记。

#Spring中的切面依赖
 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>


//Java部分
//开启切面注解
@Aspect
//lombok日志注解
@Slf4j
//注入容器,统一管理
@Component
public class WebException extends BaseApiService {

    //切入点 所有的方法 >> execution 为切面表达式 感兴趣的小伙伴可以百度
    @Pointcut("execution(* yq..*(..))")
    private void webPointcut() {
    }


//环绕增强
    @Around("webPointcut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) {
        Object proceed = null;
        String className = proceedingJoinPoint.getSignature().getDeclaringTypeName();
        String methodName = proceedingJoinPoint.getSignature().getName();
        System.out.println();
        System.out.println("============== " + className + " 类中的方法:" + methodName + " 开始执行 ==============");
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        //参数名称
        String[] parameterNames = methodSignature.getParameterNames();
        //参数值
        Object[] args = proceedingJoinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            System.out.println("第"+i+"个参数:"+"  key:"+parameterNames[i]+"  value:"+args[i]);
        }
        try {
            proceed = proceedingJoinPoint.proceed();
            System.out.println("执行结果为:" + proceed);
        } catch (Throwable e) {
            e.printStackTrace();
            System.out.println("执行方法发生了错误:"+e.getMessage());
            System.out.println("============== " + className + " 类中的方法:" + methodName + " 执行结束 ==============");
            return setResultError("执行方法发生了错误:"+e.getMessage());
        }
        System.out.println("============== " + className + " 类中的方法:" + methodName + " 执行结束 ==============");
        return proceed;
    }

}
**
首先,需要在Spring配置文件开启AOP的支持: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" 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"> <!-- AOP 配置 --> <aop:aspectj-autoproxy /> <!-- 其他 Bean 的配置 --> <!-- ... --> </beans> ``` 然后,分别使用XML和注解的方式实现各种增强: ## 前置增强 使用XML配置: ```xml <aop:config> <aop:aspect id="myAspect" ref="myAspectBean"> <aop:before pointcut="execution(* com.example.service.*.*(..))" method="beforeAdvice" /> </aop:aspect> </aop:config> ``` 使用注解: ```java @Aspect @Component public class MyAspect { @Before("execution(* com.example.service.*.*(..))") public void beforeAdvice() { // 前置增强的逻辑 } } ``` ## 后置增强 使用XML配置: ```xml <aop:config> <aop:aspect id="myAspect" ref="myAspectBean"> <aop:after-returning pointcut="execution(* com.example.service.*.*(..))" method="afterAdvice" /> </aop:aspect> </aop:config> ``` 使用注解: ```java @Aspect @Component public class MyAspect { @AfterReturning("execution(* com.example.service.*.*(..))") public void afterAdvice() { // 后置增强的逻辑 } } ``` ## 环绕增强 使用XML配置: ```xml <aop:config> <aop:aspect id="myAspect" ref="myAspectBean"> <aop:around pointcut="execution(* com.example.service.*.*(..))" method="aroundAdvice" /> </aop:aspect> </aop:config> ``` 使用注解: ```java @Aspect @Component public class MyAspect { @Around("execution(* com.example.service.*.*(..))") public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable { // 环绕增强的前置逻辑 Object result = joinPoint.proceed(); // 环绕增强的后置逻辑 return result; } } ``` ## 抛出异常增强 使用XML配置: ```xml <aop:config> <aop:aspect id="myAspect" ref="myAspectBean"> <aop:after-throwing pointcut="execution(* com.example.service.*.*(..))" method="afterThrowingAdvice" /> </aop:aspect> </aop:config> ``` 使用注解: ```java @Aspect @Component public class MyAspect { @AfterThrowing("execution(* com.example.service.*.*(..))") public void afterThrowingAdvice() { // 抛出异常增强的逻辑 } } ``` ## 最终增强 使用XML配置: ```xml <aop:config> <aop:aspect id="myAspect" ref="myAspectBean"> <aop:after pointcut="execution(* com.example.service.*.*(..))" method="finallyAdvice" /> </aop:aspect> </aop:config> ``` 使用注解: ```java @Aspect @Component public class MyAspect { @After("execution(* com.example.service.*.*(..))") public void finallyAdvice() { // 最终增强的逻辑 } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值