在Java中调用方法之前和之后运行方法

Van*_*anp 10 java reflection annotations

我试图写一个Java程序,从而调用后了methodA() ,命名为第一个方法()methodBeforeA被称为然后了methodA()被执行之后的另一种方法被称为命名,methodAfterA() .这与Junit使用Annotations(使用@ Before,@ Test,@ After)非常相似,所以我认为应该可以使用反射,但我没有很好的线索.

Mik*_*uel 5

AspectJ允许您在方法输入之前和方法退出之后指定切割点.

http://www.eclipse.org/aspectj/doc/released/progguide/starting-aspectj.html

在AspectJ中,切入点选择程序流中的某些连接点.例如,切入点

call(void Point.setX(int))
Run Code Online (Sandbox Code Playgroud)

选择每个连接点,这是一个对具有签名void的方法的调用Point.setX(int)- 即带有单个参数Pointvoid setX方法int.