开发者

Are all methods proxied when using Spring AOP?

开发者 https://www.devze.com 2022-12-20 13:54 出处:网络
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

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.


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 - Debunking myths: proxies impact performance by the Spring team about proxy performance myths


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消