I am experimenting with Spring AOP for the first time and get stuck in the XML configuration. I’m trying to get a mock version of AOP-based “logging” up and running, using a MethodInterceptor to wrap specific method calls and do some simple System.out.println statements before and after those method invocations. Simple stuff, right?
So my project has many classes, two of them are Fizz and Buzz. Fizz has a method named foo() and Buzz has a method named wapap(). Every time these methods are invoked at runtime, I want my LoggingInterceptor to execute its invoke() method around them:
public class LoggingInterceptor implements MethodInterceptor
{
public Object invoke(MethodInvocation methodInvocation)
{
try
{
System.out.println("About to call a special method.");
Object result = methodInvocation.proceed();
return result;
}
finally
{
System.out.println("Finished executing the special method.");
}
}
}
So I understand the concepts of advice (my interceptor impl), pointcuts (the methods that will have advice executed around them), and pointcut advisors (bindings between advice and pointcuts).
I’m just struggling tying it altogether in a simple XML config.
Here’s what I have so far, but I know it’s missing pointcut and pointcut advisor definitions, and possibly more.
<beans default-autowire="no" >
<bean name="loggingInterceptor" class="org.me.myproject.aop.LoggingInterceptor"/>
</beans>
What am I missing here to make this specific to Fizz::foo() and Buzz::wapap() calls?
Any nudges in the right direction are enormously appreciated!
Add this:
You also need to add AOP namespace declaration in version appropriate to your framework:
Also consider using @AspectJ aspects and see this question: Spring: Standard Logging aspect (interceptor).