Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of access modifiers in Spring AOP expression?

From the Spring doc:

6.2.3.4. Examples

Spring AOP users are likely to use the execution pointcut designator the most often. The format of an execution expression is:

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)

I can see the modifiers-pattern? where you can say public, private, protected. And on the same document it says:

6.2.3.1. Supported Pointcut Designators

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!

I'm abit confused, what is the point of using the modifiers-pattern?, please give an example?

like image 240
Carlos Jaime C. De Leon Avatar asked Oct 23 '22 14:10

Carlos Jaime C. De Leon


1 Answers

That documentation is now out of date. The latest is at https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#aop-pointcuts-designators and says

Due to the proxy-based nature of Spring’s AOP framework, calls within the target object are, by definition, not intercepted. For JDK proxies, only public interface method calls on the proxy can be intercepted. With CGLIB, public and protected method calls on the proxy are intercepted (and even package-visible methods, if necessary). However, common interactions through proxies should always be designed through public signatures.

Note that pointcut definitions are generally matched against any intercepted method. If a pointcut is strictly meant to be public-only, even in a CGLIB proxy scenario with potential non-public interactions through proxies, it needs to be defined accordingly.

If your interception needs include method calls or even constructors within the target class, consider the use of Spring-driven native AspectJ weaving instead of Spring’s proxy-based AOP framework. This constitutes a different mode of AOP usage with different characteristics, so be sure to make yourself familiar with weaving before making a decision.

So be careful with non-public access modifiers, but you can use them in certain scenarios with cglib proxies.

like image 195
artbristol Avatar answered Oct 27 '22 09:10

artbristol