java - 使用 aspectj 分析选定的方法

标签 java profiling aop aspectj

我想使用 aspectj 来分析图书馆。我的计划是用注释标记需要分析的方法:

@Profiled("logicalUnitOfWork")

然后有一个方面会在使用 logicalUnitOfWork 突出显示分析内容的方法之前和之后触发。

所以,我的切入点看起来像这样。请注意,我这里没有注释的参数;这是我不确定该怎么做的事情之一:

pointcut profiled() : execution(@Profiled * *());

before() : profiled () {
    // : the profiled logical name is in this variable:
String logicalEventType;
Profiler.startEvent (logicalEventType);
}

after() returning : profiled() {
    // : the profiled logical name is in this variable:
String logicalEventType;
    Profiler.endEvent (logicalEventType);
}

被剖析的方法定义如下:

@Profiled("someAction")
public void doAction (args...) {}

简而言之,如何将@Profiled注解的值获取到切面中?我不需要根据值限制发生哪些分析,我只需要它对建议可见。此外,我是否需要将注释的保留设置为运行时才能使其正常工作,或者我是否可以改为使用类级别的保留?

最佳答案

我不确定这是否是最好的方法,但您可以尝试类似的方法:


   pointcut profiledOperation(Profiled p) : 
      execution(@Profiled * *()) && @annotation(p);

   before(Profiled p): profiledOperation(p)
   {
      System.out.println("Before " + p.value());
   }

   after(Profiled p): profiledOperation(p)
   {
      System.out.println("After " + p.value());
   }

由于您需要在运行时访问注释值,因此您必须将 @Retention 设置为 RUNTIME

关于java - 使用 aspectj 分析选定的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/538010/

相关文章:

java - 在 Internet Explorer 中使用 ServletOutputStream 通过 HTTPS 从 Servlet 返回 CSV 文件

java - 在 Java EE 应用程序之间进行同步调用的最佳方式

linux - 用于分析结果的基于文本的查看器

python - 分析和查找 flask 应用程序的瓶颈——当前响应时间为 30 秒

Android 以 AOP 风格编程实现分析

c++ - AspectC++ 入门

java - 如何在不更改代码的情况下将限制应用于 spring/hibernate 企业应用程序中的所有业务点?

java - 如何在 Web 服务中序列化公共(public)静态最终字符串?

java - 在多线程环境中重绘

c++ - 非常困倦地分析Qt应用程序,如何分析结果?