Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why protected methods are not intercepted by Spring AOP

I am familiar with Spring AOP. As i read in the spring documentation http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/aop.html, Spring AOP works on the concept of proxies.

In the section 8.2.3.1 Supported Pointcut Designators, i found the below note

Due to the proxy-based nature of Spring's AOP framework, protected methods are by definition not intercepted, neither for JDK proxies (where this isn't applicable) nor for CGLIB proxies (where this is technically possible but not recommendable for AOP purposes). As a consequence, any given pointcut will be matched against public methods only!

At first, I didn't believe it, so i tried to implement it without using interfaces where all methods are public by default and was surprised that the above is true. Since proxy classes are subclasses of the advised/target object and protected methods can be accessed by a subclass so I thought protected methods would work fine.

Can someone please let me know why protected methods are not intercepted? Am I missing something?

like image 691
Anand Avatar asked Apr 04 '14 11:04

Anand


1 Answers

JDK proxies are based on interfaces and this means that all implemented methods will be public

like image 128
Evgeniy Dorofeev Avatar answered Oct 01 '22 03:10

Evgeniy Dorofeev