之前在Java中调用方法后运行的方法 [英] Run a method before and after a called method in Java
问题描述
我试图写一个Java程序,这样调用的了methodA()的,首次命名方法的 methodBeforeA()的叫,然后的之后了methodA ()的被执行之后的另一种方法被称为命名的 methodAfterA()的。这非常类似于Junit的不使用注释(使用@Before,@测试,@After),所以我认为应该使用反射是可能的,但我没有一个很好的线索。
I'm trying to write a Java program such that after calling a methodA(), first a method named methodBeforeA() is called and then methodA() gets executed followed by another method being called named, methodAfterA(). This is very similar to what Junit does using Annotations (using the @Before, @Test, @After), so i think it should be possible using reflection but i don't have a very good clue.
推荐答案
<一个href=\"http://thejava$c$cmonkey.blogspot.com/2007/08/using-aspectj-to-detect-violations-of.html\">AspectJ让你的方法入境前和方法后退出指定分割点。
AspectJ allows you to specify cutpoints before method entry and after method exit.
<一个href=\"http://www.eclipse.org/aspectj/doc/released/progguide/starting-aspectj.html\">http://www.eclipse.org/aspectj/doc/released/progguide/starting-aspectj.html
在AspectJ中,切入点在程序流程挑选出一定的连接点。例如,切入点
In AspectJ, pointcuts pick out certain join points in the program flow. For example, the pointcut
call(void Point.setX(int))
挑选出每个连接点那就是具有签名无效的方法调用 Point.setX(INT)
- 即点
的无效setX的
方法,一个 INT
参数。
picks out each join point that is a call to a method that has the signature void Point.setX(int)
— that is, Point
's void setX
method with a single int
parameter.
这篇关于之前在Java中调用方法后运行的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!