AOP caveats using Spring

While using Spring AOP be aware that…

  • If a method A calls a method B in the same instance and both are advised by the same pointcut, the advice for method B will not be triggered. This is due to the dynamic nature of AOP implementation and instance proxying (cfr. here, here). An ugly workaround: hold a reference of the current object instance and invoke other methods using that reference (e.g. me().methodB() instead of this.methodB()).

  • If your pointcuts use annotations method annotations are not inherited (and not inheritable).

  • If your advised object does not implement an interface, then it will be wrapped in a proxy via CGLIB. In this case its class must not be final and it must have a default constructor (min access protected). Spring kindly warns you if CGLIB jars are needed and they are not in the classpath.

AOP and Spring reference.