I wrote a simple Spring2.5 application to demonstrate / test AOP; in particular, I want to register a record and exit from each method of each class in a specific package. This is what I have ...
(note: I use annotation controllers, I do not specify details that are not directly related to aop, because my basic setup works fine - I only include aup-related information - let me know if you need to see more)
applicationContext.xml:
(...) <bean id="loggerInterceptor" class="aspect.LoggerInterceptor" /> (...)
dispatcher-servlet.xml:
(...) <aop:aspectj-autoproxy proxy-target-class="true" /> (...)
HomeController.java:
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()..."); } }
LoggerInterceptor.java:
public class LoggerInterceptor { @Pointcut("execution(* controller.*.*(..))") private void anyOperationInControllerPackage() { } @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; } } }
Here is what I get when calling HomeController # get ():
Entering controller.HomeController#get() using arguments: [] In HomeController#get()... In HomeController#somePrivateMethod()... In HomeController#somePublicMethod()... Leaving controller.HomeController#get().
As you can see, the only way to intercept is HomeController # get (). When #get () calls #somePrivateMethod () or #somePublicMethod (), the interceptor does not detect them. I would expect at least that #somePublicMethod () would also be caught (and since I use cglib, I would also expect #somePrivateMethod () to be caught).
So, I think, my question is what I need to change / add to allow (at least) all public methods in the controller package to be caught, even when another method in this package called them and the first one was caught ?? ?
Hope this makes sense. : D
EDIT (25APR2011 @ 13: 13)
applicationContext.xml:
(...) <context:load-time-weaver /> <bean id="loggerInterceptor"... /> (...)
aop.xml:
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd"> <aspectj> <weaver> <include within="controller.*" /> </weaver> <aspects> <aspect name="aspect.LoggerInterceptor" /> </aspects> </aspectj>
In the "Netbean Project Properties" on the "Run" tab, I added this line to the "VM Settings" :
-javaagent:C:\Users\bgresham\Documents\libraries\spring-framework-2.5\dist\weaving\spring-agent.jar
As before, I do not get any errors - I just do not get the "attached" files that I am looking for.
???