Wednesday, 30 November 2011

Spring proxies: JDK vs. CGLib

This page contains an excellent and succinct summary of Spring Proxies:

http://insufficientinformation.blogspot.com/2007/12/spring-dynamic-proxies-vs-cglib-proxies.html

You could read a chapter in most Spring books and learn less.  Heed the advice: "Most important proxy-based AOP only works from "outside". Internal method calls are never intercepted."  If you don't already understand this, take the time to do so.

The Spring reference manual is also a good read:

http://static.springsource.org/spring/docs/3.0.0.RELEASE/spring-framework-reference/html/aop.html#aop-proxying

"...once the call has finally reached the target object, the SimplePojo reference in this case, any method calls that it may make on itself, such as this.bar() or this.foo(), are going to be invoked against the this reference, and not the proxy. This has important implications. It means that self-invocation is not going to result in the advice associated with a method invocation getting a chance to execute.

Okay, so what is to be done about this? The best approach (the term best is used loosely here) is to refactor your code such that the self-invocation does not happen."

No comments:

Post a Comment