使用不带Spring的AspectJ日志记录

问题描述 投票:9回答:4

我刚刚正在处理日志较差或没有日志的旧应用程序。它没有实现Spring框架。没有Spring,是否可以实现AspectJ日志记录功能?如果是,请给我建议一些好的教程。

java spring aspectj
4个回答
9
投票

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

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

也请参考链接以获取有关在不使用spring的情况下使用aspectj ltw的详细信息http://www.eclipse.org/aspectj/doc/next/devguide/ltw.html


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);
    }
}

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

您可以根据需要使用加载时间或编译时间进行编织。希望这会有所帮助。


0
投票

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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.