Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring AOP: logging and nested-methods
    text
    copied!<p>I have written a simple Spring2.5 app to demo/test AOP; specifically, I want to log the entry and exit of every method of every class in a specific package. This is what I have...</p> <p>(note: I am using annotation-controllers; I am omitting details not directly-related to aop because my basic setup works fine -- I am only including aop-related details -- let me know if you need to see more)</p> <hr> <p><em><strong>applicationContext.xml :</strong></em></p> <pre><code>(...) &lt;bean id="loggerInterceptor" class="aspect.LoggerInterceptor" /&gt; (...) </code></pre> <p><em><strong>dispatcher-servlet.xml :</strong></em></p> <pre><code>(...) &lt;aop:aspectj-autoproxy proxy-target-class="true" /&gt; (...) </code></pre> <p><em><strong>HomeController.java :</strong></em></p> <pre><code>public class HomeController() { public HomeController() { } public ModelAndView get() { System.out.println("In HomeController#get()..."); this.somePrivateMethod(); this.somePublicMethod(); return new ModelAndView( "home" ); } private void somePrivateMethod() { System.out.println("In HomeController#somePrivateMethod()..."); } public void somePublicMethod() { System.out.println("In HomeController#somePublicMethod()..."); } } </code></pre> <p><em><strong>LoggerInterceptor.java :</strong></em></p> <pre><code>public class LoggerInterceptor { @Pointcut("execution(* controller.*.*(..))") private void anyOperationInControllerPackage() { /* nothing to do here; * this just defines that we want to catch all methods * in the controller-package */ } @Around("anyOperationInControllerPackage()") public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("Entering " + joinPoint.getSignature().getDeclaringTypeName() + "#" + joinPoint.getSignature().getName() + "() using arguments: " + Arrays.toString( joinPoint.getArgs() ) ); try { Object result = joinPoint.proceed(); System.out.println("Leaving " + joinPoint.getSignature().getDeclaringTypeName() + "#" + joinPoint.getSignature().getName() + "()." ); return result; } catch (Throwable ex) { ex.printStackTrace(); throw ex; } } } </code></pre> <hr> <p>Here is what I'm getting when HomeController#get() is invoked:</p> <pre><code>Entering controller.HomeController#get() using arguments: [] In HomeController#get()... In HomeController#somePrivateMethod()... In HomeController#somePublicMethod()... Leaving controller.HomeController#get(). </code></pre> <p>As you can see, the only method that's getting intercepted is HomeController#get(). When #get() calls #somePrivateMethod() or #somePublicMethod(), the interceptor doesn't catch those. I would expect, at the very least, that #somePublicMethod() would also get caught (and since I'm using cglib, I would also expect that #somePrivateMethod() would get caught).</p> <p>So I guess my question is what do I need to change/add in order to allow (at the very least) all public methods in the controller-package to get caught even when another method in that package called them and was itself caught first???</p> <p>I hope that makes sense. :D</p> <hr> <h2>EDIT(25APR2011 @ 1:13PM)</h2> <p><em><strong>applicationContext.xml :</em></strong></p> <pre><code>(...) &lt;context:load-time-weaver /&gt; &lt;!-- added --&gt; &lt;bean id="loggerInterceptor"... /&gt; (...) </code></pre> <hr> <p><em><strong>aop.xml :</em></strong></p> <pre><code>&lt;!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd"&gt; &lt;aspectj&gt; &lt;weaver&gt; &lt;!-- only weave classes in this package --&gt; &lt;include within="controller.*" /&gt; &lt;/weaver&gt; &lt;aspects&gt; &lt;!-- use only this aspect for weaving --&gt; &lt;aspect name="aspect.LoggerInterceptor" /&gt; &lt;/aspects&gt; &lt;/aspectj&gt; </code></pre> <hr> <p><em><strong>In Netbean's "Project Properties" under the "Run" tab I added this line to the "VM Options"</em></strong> :</p> <pre><code>-javaagent:C:\Users\bgresham\Documents\libraries\spring-framework-2.5\dist\weaving\spring-agent.jar </code></pre> <hr> <p>As before, I'm not getting any errors -- I just don't get the "nested"-logging I'm looking for.</p> <p>???</p>
 

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