springboot使用aop

文章介绍了如何在SpringBoot中使用AOP(面向切面编程)进行功能增强,通过添加依赖、定义切面类以及设置通知(Before、After、Around、AfterThrowing)来实现对目标类方法的监控和处理,展示了AOP在日志记录和事务管理中的应用。
摘要由CSDN通过智能技术生成


前言

aop的理念是增强原有业务,对原业务无侵入;经常被使用于日志和事务处理。


一、pom引入

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

在这里插入图片描述

二、编写案例

1.目标类

package com.student.controller;

import org.json.JSONObject;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Create by zjg on 2023/5/1
 */
@RestController
public class SumController {
    @RequestMapping("sum/{a}")
    public String sum(@PathVariable Integer a,@RequestParam Integer b){
        System.out.println("a+b="+(a+b));
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("sum",a+b);
        return jsonObject.toString();
    }
}

2.切面类

package com.student.sys.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
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.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * Create by zjg on 2023/5/1
 */
@Aspect
@Component
public class SumAop {
    @Pointcut("execution(* sum(..))")
    public void pointCut() {
        System.out.println("Hello Aop! pointCut");
    }
    @Before("pointCut()")
    public void before(JoinPoint joinPoint){
        System.out.println("Hello Aop! before");
    }
    @After("pointCut()")
    public void after(JoinPoint joinPoint){
        System.out.println("Hello Aop! after");
        //throw new RuntimeException("after过程出现异常!");
    }
    @Around("pointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("Hello Aop! around");
        System.out.println("环绕查看目标类:"+point.getTarget().getClass());
        System.out.println("环绕查看目标方法:"+point.getSignature());
        System.out.println("环绕查看目标参数:"+point.getArgs());
        return point.proceed();
    }
    @AfterThrowing(value="pointCut()", throwing="exception")
    public void afterThrowing(Exception exception){
        System.out.println("Hello Aop! afterThrowing");
        System.out.println("exception:"+exception.getMessage());
    }
}

3.输出

Hello Aop! around
环绕查看目标类:class com.student.controller.SumController
环绕查看目标方法:String com.student.controller.SumController.sum(Integer,Integer)
环绕查看目标参数:[Ljava.lang.Object;@68651ee8
Hello Aop! before
a+b=2
Hello Aop! after

总结

execution可以匹配包、类、方法、参数和返回值,搭配起来很灵活;
也可以根据注解匹配切面:@Around(“@annotation(org.aspectj.lang.annotation.Around)”)
参考文档
回到顶部

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值