Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to intercept static methods in Spring?

Subject line basically says it all. I have a static method I want to intercept so that around advice can be applied to it. I can get this to work with any non-static methods but I'm unsure how to allow static methods to be intercepted.

like image 204
AHungerArtist Avatar asked Feb 14 '11 15:02

AHungerArtist


People also ask

Can static methods be invoked?

It may be null." So, instead of passing in an actual object, a null may be passed; therefore, a static method can be invoked without an actual instance of the class.

Does AOP work on static methods?

Yes we can. Because we are accessing the class level members.

Can static methods take arguments?

Like a mathematical function, a Java static method can take on more than one argument, and therefore can have more than one parameter variable.

Can a Spring Bean have static methods?

Yes, A spring bean may have static methods too.


1 Answers

You can't do that with Spring AOP, because it is proxy based. You have to use AspectJ. Take a look at this simple example: http://blog.jayway.com/2007/02/16/static-mock-using-aspectj/

like image 76
limc Avatar answered Sep 28 '22 04:09

limc