Why protected methods are not intercepted by Spring AOP

I am familiar with Spring AOP . Since I read in the spring documentation http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/aop.html , Spring AOP working on a proxy concept.

In section 8.2.3.1 Supported pointcut pointers , I found the following note

Due to the foundation based on the spring AOP proxy structure, the secure methods by definition are not intercepted, neither for the JDK proxy (if not applicable), nor for the CGLIB proxy (where it is technically possible, but not recommended for AOP purposes). As a result, any given pointcut will be matched with public methods only!

At first I did not believe, so I tried to implement it without using interfaces, where all methods are publicly accessible by default, and was surprised that this is true. Since proxy classes are subclasses of the recommended / target object, and protected methods can be accessed by a subclass, so I thought that protected methods would work fine.

Can someone please let me know why protected methods are not intercepted? Did I miss something?

+6
source share
2 answers

JDK proxies are interface-based, which means that all implemented methods will be publicly available.

0
source

In fact, in the latest spring documentation it is obvious that this is possible with cglib

Due to the proxy-based Spring s AOP framework, calls to the target are, by definition, not intercepted. For JDK proxies, only calls to the open interface method on the proxy can be intercepted. Using CGLIB, calls to the general and protected method on the proxy will be intercepted and even displayed as packets if necessary. However, general proxy interactions should always be developed using public signatures.

Since they mention that public and package-visible are included in the use of cglib, try using the latests spring version, they could update such things accordingly

check here

0
source

All Articles