Spring AOP Pointcut for Serializable

aop, java, serializable, spring, spring-aop

Solution

The thing is: Spring AOP won't be able to match these pointcuts. Spring AOP only matches pointcuts on public methods. You will need AspectJ compilation or Load Time Weaving to get this to work.

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!

If your interception needs include protected/private methods or even constructors, 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 first before making a decision.

Source: 8.2.3.1. Supported Pointcut Designators

And before you ask: making the methods public won't help either, because they will be called by the Java serialization mechanism, not by Spring, so Spring AOP is not aware of what's happening.

Problem

Is it possible to create a pointcut for SpringBean Serializable ? I want to intercept writeObject and readObject of my controllers with the syntax: ``` execution(* ((@org.springframework.stereotype.Controller java.io.Serializable)+).*(..)) ``` I think the problem is readObject/writeObject is private and called in a different way. Any suggestion ?

Original source