views:

170

answers:

2

When using Spring AOP to create a proxy for a class using NameMatchMethodPointcutAdvisor and BeanNameAutoProxyCreator does this essentially proxy every call to the object, but only apply the advice to the matched methods, or somehow create a Proxied object that only has those methods and uses the normal object for the calls that are supposed to be intercepted? The way, I think I understand it is that it does proxy every call to the object but then only calls the Advisor on the methods that match - but I can't find a good example/post to confirm this. Thanks.

+3  A: 

Depends on the technique used. (It is configurable by an attribute proxy-target-class in your aop config)

  • JDK dynamic proxies are proxies by interface - each methods of the interface goes through the proxy, as you said, and if it matches happens to be an "advised" method, the advisor is applied. Otherwise it is delegated to the original object

  • CGLIB proxies are effectively subclasses defined at runtime of your concrete classes. I can't be sure in this, but I assume only the "advised" methods are overridden, the rest retain the definition of the superclass.

However, no matter which mechanism is used:

  • it isn't your concern how exactly are the proxies implemented
  • this doesn't impact performance in a significant way - read this article by the spring team about proxy performance myths
Bozho
Just finished reading that article (and a few others) before I posted this question. Wasn't worried about the performance costs since I'm saving trips to the database which will be many orders of magnitude slower then a thousand proxy calls. Thanks for the answer.
Gandalf
A: 

or somehow create a Proxied object that only has those methods and uses the normal object for the calls that are supposed to be intercepted?

How would this actually work? When a class has a reference to the class that is being proxied, it only has one reference to it. It either has to invoke a proxy class or a non-proxied class. Spring can't know which methods you are calling and therefore can't give you one type if you need to call the advised method and another type if you're not.

matt b