当前位置: 首页 > 知识库问答 >
问题:

使用Spring AOP记录Spring framework类的方法

司马高韵
2023-03-14

是否有方法使用Spring AOP记录Spring Framework类下方法的进入和退出?
例如:
我希望在org.SpringFramework.Context.Support.AbstractApplicationContext类中记录
Refresh()的退出和退出


我尝试了以下操作。但没有运气:(

@Around("execution(* org.springframework.context.*(..))")
public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
    System.out.println("Entry Into Method : " + joinPoint.getSignature().getName());
    System.out.println("Arguments : " + Arrays.toString(joinPoint.getArgs()));

    joinPoint.proceed();

    System.out.println("Exit from Method : " + joinPoint.getSignature().getName());
}


但我最终遇到了bean创建异常。

共有1个答案

庞书
2023-03-14

我认为这是行不通的,只是因为您试图截取类上的一些方法,在创建后(以及调用refresh())它开始创建AOP所需的bean。

我相信您需要使用AspectJ。像下面的myaspect.aj一样定义为方面:

package com.foo.bar;

public aspect MyAspect {

    static final void println(String s) {
        System.out.println(s);
    }

    pointcut aroundAppContext():
        execution(* org.springframework.context.support.*.*(..));

    Object around(): aroundAppContext() {
        println("Intercepted message: " + thisJoinPointStaticPart.getSignature().getName());
        println("in class: " + thisJoinPointStaticPart.getSignature().getDeclaringType().getName());
        Object result = proceed();
        println("  result: " + result);
        return result;
    }

}

在项目中定义具有以下内容的meta-inf/aop.xml文件:

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>

    <weaver> <!-- options="-debug -verbose -showWeaveInfo"> -->
        <!-- only weave classes in our application-specific packages -->
        <include  within="org.springframework.context.support.*"/>
    </weaver>

    <aspects>
        <!-- weave in just this aspect -->
        <aspect name="com.foo.bar.MyAspect"/>
    </aspects>

</aspectj>
 类似资料:
  • 问题内容: 是否有可能在springframework中记录方法所花费的时间?全部]自动。我的意思是,我不想进入每种方法并编写log.debug(“ ....”); 东西。 问题答案: AOP是您在这里需要的。AOP允许您将代码添加到您的应用程序,而无需修改原始代码。Spring AOP倾向于使用 Proxy 对象来完成此任务。 代理 对象使用装饰器模式包装原始 Target 对象并添加代码。所述

  • 一些日志记录需要在类的静态方法执行前后完成。我试图使用Spring AOP实现这一点,但它不起作用,对于普通方法来说,它是起作用的。请帮助我理解如何实现这一点,如果可以使用注释来完成,那就太好了。

  • 问题内容: 我想重写我的日志记录类,我想知道如何在一个快速文件中替换 PRETTY_FUNCTION 或NSStringFromSelector(_cmd)以便跟踪方法调用? 问题答案: 查看我刚刚发布的新库:https://github.com/DaveWoodCom/XCGLogger 这是Swift的调试日志记录库。 能够使用宏的关键是将它们设置为日志记录功能的默认值。然后,编译器将使用期望

  • 本文向大家介绍js使用cookie记录用户名的方法,包括了js使用cookie记录用户名的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了js使用cookie记录用户名的方法。分享给大家供大家参考,具体如下: cookie思路:当点击登录按钮时,把cookie存起来,再次访问的时候就读取cookie,即把txt的value设置之前存起来cookie的值就行。 希望本文所述对大家Java

  • 出身背景 我使用的是Postsharp版本3.0.42.9,并创建了一个自定义跟踪器属性(

  • 本文向大家介绍kvm中使用console命令记录的方法,包括了kvm中使用console命令记录的方法的使用技巧和注意事项,需要的朋友参考一下 前言 在工作中,我们可能都会接触到 KVM 虚拟机,并且公司的很多应用也都会跑在 KVM 虚拟机上。因此,对 KVM的熟练应用,也是运维必不可少的能力之一。那么在 KVM 的实践过程中,我们肯定会经常去思考如何能够更深入、更标准的实践好 KVM。由此,就引