Spring AOP Framework |
Here's my little exploration to Spring's AOP framework - a little
interceptor which just logs which class is called and which method is called, plus
logging the method invocation time; however I hope this can help others to
understand Spring's AOP and help them to write interceptors of their own.
-cptechno
An interceptor used in Spring need
to implement the org.aopalliance.intercept.MethodInterceptor interface, which requires
implementing this method:
|
|
And next, comes that little interceptor...
|
|
You can do anything as you like; but pay attention to these two lines:
|
|
The execution sequence is as follows:
Object retVal =
methodInvocation.proceed(); Object retVal =
methodInvocation.proceed();, which gives control to the next interceptor in the interceptor
stack, or the underlying method. return retVal; return retVal;, which returns control to
the interceptor above it, or exit the whole interceptor stack.Next, to use the interceptor we wrote,
we need to turn our business object as an AOP target, like this:
|
|
As shown, we just need to change the bean's id.
Next we need to hang the interceptor on to Spring's ApplicationContext.
|
|
And the last step, we declare our business object actually in the
ApplicationContext, via its interface we defined, via Spring's
ProxyFactoryBean.
|
|
On the application code that will
access the business object, no changes are necessary.
Then at your logging target (console, file, etc...) you can see the following
output similar to this (time and level info trimmed here):
|
|
I found it
very similar to Nanning's way.
http://www.mackmo.com/nick/blog/java/?permalink=aop-frameworks-review.txt