随笔 - 502  文章 - 1 评论 - 6 阅读 - 37万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

基础文献   

    https://blog.csdn.net/abcd898989/article/details/50809321

 

简单Demo配置

  pom.xml

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

  主类上加上Aop注解

//Aop代理
@EnableAspectJAutoProxy(proxyTargetClass=true, exposeProxy = true)

 

复制代码
package com.cjcx.pay.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Slf4j
@Aspect
@Component
public class ServiceMonitor {

    /**
     * 前置通知:目标方法执行之前执行以下方法体的内容
     *
     * @param joinPoint
     */
    @Before("execution(* com..*Service.demo(..))")
    public void Before(JoinPoint joinPoint) {
        log.info("Before Completed: " + joinPoint);
        log.info("joinPoint toString(): " + joinPoint.toString());

        Object obj = joinPoint.getThis();
        log.info("joinPoint getThis: " + obj);

        Object target = joinPoint.getTarget();
        log.info("joinPoint getTarget: " + target);

        Signature signature = joinPoint.getSignature();
        log.info("joinPoint getSignature: " + signature.toString());
        log.info("joinPoint signature getName: " + signature.getName());
        log.info("joinPoint signature getModifiers: " + signature.getModifiers());
        log.info("joinPoint signature getDeclaringType: " + signature.getDeclaringType());
        log.info("joinPoint signature getDeclaringTypeName: " + signature.getDeclaringTypeName());
        log.info("joinPoint getArgs: " + Arrays.asList(joinPoint.getArgs()));
    }


    /**
     * 后置通知:目标方法执行之后执行以下方法体的内容,不管是否发生异常。
     *
     * @param joinPoint
     */
    @After("execution(* com..*Service.demo(..))")
    public void After(JoinPoint joinPoint) {
        log.info("After Completed: " + joinPoint);
    }

    /**
     * 返回通知:目标方法正常执行完毕时执行以下代码
     *
     * @param joinPoint
     */
    @AfterReturning(value = "execution(* com..*Service.demo(..))", returning = "result")
    public void AfterReturning(JoinPoint joinPoint, Object result) {
        log.info("AfterReturning Completed: " + joinPoint);
        log.info("AfterReturning returning result" + result);
    }


    /**
     * 异常通知:目标方法发生异常的时候执行以下代码
     *
     * @param joinPoint
     */
    @AfterThrowing(value = "execution(* com..*Service.demo(..))", throwing = "e")
    public void AfterThrowing(JoinPoint joinPoint, Exception e) {
        log.info("AfterThrowing Completed: " + joinPoint);
        log.info("AfterThrowing Exception: " + e.getMessage());
    }

    /**
     * 环绕通知:目标方法执行前后分别执行一些代码,发生异常的时候执行另外一些代码
     *
     * @param jp
     */
    /*@Around("execution(* com..*Service.demo(..))")
    public Object Around(ProceedingJoinPoint jp) {
        log.info("Around Completed: " + jp);
        String methodName = jp.getSignature().getName();
        Object result = null;
        try {
            System.out.println("【环绕通知中的--->前置通知】:the method 【" + methodName + "】 begins with " + Arrays.asList(jp.getArgs()));
            //执行目标方法
            result = jp.proceed();
            System.out.println("【环绕通知中的--->返回通知】:the method 【" + methodName + "】 ends with " + result);
        } catch (Throwable e) {
            System.out.println("【环绕通知中的--->异常通知】:the method 【" + methodName + "】 occurs exception " + e);
        }

        System.out.println("【环绕通知中的--->后置通知】:-----------------end.----------------------");
        return result;
    }*/


}
复制代码

 

posted on   1161588342  阅读(125)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2017-09-07 Redis 命令
2017-09-07 Redis 安装 和 启动
2017-09-07 Mongodb 安装 和 启动
2017-09-07 JS 报表制作
2017-09-07 Reactjs 打包后 Tomcat 部署 404问题
2017-09-07 npm 安装cnpm
2017-09-07 Fiddler 链接到逍遥安卓模拟器
点击右上角即可分享
微信分享提示