Note that there are some explanatory texts on larger screens.

plurals
  1. POSecond invocation of Aspectj-adviced method in is ignored
    primarykey
    data
    text
    <p>I am in the process of adding some <em>AspectJ</em> advices in my <em>Spring</em>-based Java project. When I run an intercepted method once, it all works fine (i.e., the advice is executed). However, the next invocation of the very same method does not go through the proxy anymore.</p> <p>This is my test code:</p> <pre><code>@Test public void testFooOperationIsAdviced() throws Exception { TestController testController = appContext.getBean("testController"); testController.foo(); testController.foo(); } </code></pre> <p>Here's the <code>foo()</code> method:</p> <pre><code>@Protect() public void foo() { System.err.println("foo!") } </code></pre> <p>And this is the relevant part in my Spring configuration:</p> <pre><code>&lt;aop:aspectj-autoproxy /&gt; &lt;bean name="myAdvice" class="mypackage.MyAdvice"/&gt; &lt;bean id="testController" class="mypackage.MyTestControllerImpl" /&gt; &lt;aop:config&gt; &lt;aop:aspect id="protectAspect" ref="myAdvice"&gt; &lt;aop:pointcut id="annotatedController" expression="execution(public * mypackage.*+.*(..)) and @annotation(protect)" /&gt; &lt;aop:around pointcut-ref="annotatedController" method="applyProtectionRules" arg-names="protect"/&gt; &lt;/aop:aspect&gt; &lt;/aop:config&gt; </code></pre> <p>The aspect is currently just doing <code>System.err.println("advice")</code> and <code>pp.proceed()</code>.</p> <p>So, if we execute the test above, you'd expect</p> <blockquote> <p>advice foo! advice foo!</p> </blockquote> <p>However, what I get is:</p> <blockquote> <p>advice foo!</p> </blockquote> <p>The second invocation never gets to the advice! And, what is worse, <em>the target method is not even executed</em>.</p> <p>Do you have any clue as to why this happens?</p> <p>Note 1: To make things worse: sometimes, when I execute with the debugger and go step by step, it does work normally. No kidding...</p> <p>Note 2: If there are typos in the config, they are just typos, since I adapted the original code to make it simpler. Take into account that the aspect does work for the first invocation.</p> <p>Note 3: I do want to stick to Spring. I can't have the pointcuts hardcoded in the Java code, since I want the library user to provide their own, and the only way I can think of is let them define the <code>aop:config</code> block.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload