使用 aspectj 分析选定的方法 [英] Use aspectj to profile selected methods

查看:22
本文介绍了使用 aspectj 分析选定的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 aspectj 来分析一个库.我的计划是用注释标记需要分析的方法:

@Profiled("logicalUnitOfWork")

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

所以,我的切入点看起来像这样.请注意,我这里没有注释的论据;这是我不知道该怎么做的事情之一:

切入点 profiled() : execution(@Profiled * *());之前():异形(){//: 分析的逻辑名称在这个变量中:字符串逻辑事件类型;Profiler.startEvent (logicalEventType);}after() 返回:profiled() {//: 分析的逻辑名称在此变量中:字符串逻辑事件类型;Profiler.endEvent(logicalEventType);}

被分析的方法定义如下:

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

简而言之,如何将 @Profiled 注释的值放入方面?我不需要根据值限制进行哪些分析,我只需要它对建议可见.另外,我是否需要将注释的保留设置为运行时才能使其工作,还是可以改为使用类级别的保留?

解决方案

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

<前><代码>切入点 profiledOperation(Profiled p) :执行(@Profiled * *()) && @annotation(p);before(Profiled p): profiledOperation(p){System.out.println("之前" + p.value());}after(Profiled p): profiledOperation(p){System.out.println("之后" + p.value());}

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

I'd like to use aspectj to profile a library. My plan was to mark methods that require profiling with an annotation:

@Profiled("logicalUnitOfWork")

And then have an aspect that would fire before and after methods that would use the logicalUnitOfWork to highlight the profiled content.

So, my pointcut to start with looks like this. Note that I don't have the argument for the annotation here; that's one of the things I'm not sure how to do:

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

The methods being profiled would be defined like this:

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

In short, how can I get the value of the @Profiled annotation into the aspect? I don't need to restrict which profiling occurs based on the value, I just need it to be visible to the advice. Also, do I need to have the annotation's retention set to runtime for this to work, or can I have class-level retention instead?

解决方案

I am not sure if this is the best way to do it, but you could try something like:


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

Since you need to access the annotation value at runtime you will have to set the @Retention to RUNTIME.

这篇关于使用 aspectj 分析选定的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆