使用没有Spring的AspectJ日志记录

时间:2012-08-13 16:34:32

标签: java spring aspectj

我刚刚处理了一个日志不好或没有日志的旧应用程序。它没有实现Spring框架。
是否可以在没有Spring的情况下实现AspectJ日志记录功能? 如果是,请向我推荐一些好的教程。

5 个答案:

答案 0 :(得分:8)

尝试此链接以显示一个简单的应用程序,显示使用加载时间编织而不使用Spring http://ganeshghag.blogspot.in/2012/10/demystifying-aop-getting-started-with.html

所需要的只是aspectj运行时和weaver jar,以及包含正确配置的META-INF \ aop.xml文件。

还可以参考链接了解有关使用aspectj ltw而不使用spring的详细信息 http://www.eclipse.org/aspectj/doc/next/devguide/ltw.html

答案 1 :(得分:4)

您可以使用没有Spring(或log4j)的aspectj来记录任何aspectj支持的连接点的消息,

例如,

public aspect ListAllMethodExecution {
    private int callDepth; 

    private pointcut executionJoinPoints(): !within(ListAllMethodExecution) && execution (* *.*(..));

    before(): executionJoinPoints(){
        print("Before call " + thisJoinPoint);
        callDepth++;
    }

    after(): executionJoinPoints(){
        callDepth--;
        print("After call " + thisJoinPoint);
    }

    private void print(String s){
        for(int i=0; i<callDepth; i++)
            System.out.print("    ");
        System.out.println(s);
    }
}

您可以修改切入点表达式,以便记录您可能感兴趣的特定事件或其他静态连接点上的特定包。 您也可以根据需要重定向日志来修改打印方法。

您可以使用加载时间或编译时编织。希望这会有所帮助。

答案 2 :(得分:1)

试试load time weaving with spring

  

加载时编织(LTW)是指在将AspectJ方面加载到Java虚拟机(JVM)中时将其编织到应用程序的类文件中的过程。

这意味着您可以向类添加日志记录方面,并将其添加到Spring IoC容器外部的对象。

答案 3 :(得分:0)

也许这个OpenSource可以帮到你。 https://code.google.com/p/perfspy/。它是运行时日志记录,性能监视和代码检查工具。它使用ApsectJ在运行时编织应用程序代码,并记录每个方法的执行时间及其输入参数和值。它有一个UI应用程序,您可以在其中查看方法调用及其输入和返回值作为树。有了它,您可以发现性能瓶颈并理解复杂的代码流。

答案 4 :(得分:0)

尝试https://devkonline.com/tutorials/content/aspectj-java。在Java中使用Aspectj有逐步的解释

相关问题