Abhishek
Abhishek

Reputation: 91

How to advise static methods using Spring AOP?

Some logging needs to be done before and after execution of static method of a class. I tried to achieve this using Spring AOP but it is not working and for normal methods it is working. Please help me in understanding how to achieve that and it will be great if it can be done using annotations.

Upvotes: 2

Views: 6875

Answers (1)

kriegaex
kriegaex

Reputation: 67297

Maybe you should just read the documentation before using Spring AOP. This statement is true for any tool. If you read it, you will notice that Spring AOP is based on dynamic proxies, which means it only works for

  • non-static public methods (also protected or package-scoped ones if you use CGLIB instead of JDK proxies)
  • on Spring beans.

The manual also states that you can use full AspectJ via load-time weaving (LTW) if you need a more powerful AOP solution. In that case you can also intercept static methods and many more things which Spring AOP users could only dream of.

Upvotes: 8

Related Questions