Carlos Jaime C. De Leon
Carlos Jaime C. De Leon

Reputation: 2896

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?

Upvotes: 5

Views: 2958

Answers (2)

artbristol
artbristol

Reputation: 32407

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.

Upvotes: 4

Nandkumar Tekale
Nandkumar Tekale

Reputation: 16158

You use access modifiers in relation to your pointcut declarations in order to control where your pointcut declarations are visible within your application.

Pointcut declarations have the same access modifiers as regular Java methods :

  1. public, the pointcut delcaration is visible throughout your entire applications aspects;
  2. default (no modifier specified), the pointcut declaration is visible to all other aspects in the same package;
  3. protected, the pointcut delcaration is visible only to subaspects;
  4. private, the pointcut delcaration is only visible in the aspect within it is declared.

Upvotes: 0

Related Questions