Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't we use @Cacheable with static method in spring with ehcache?

I am new to Spring and read that we can't use @Cacheable with static method but can't found why we can't use so any one can explain so that beginner can understood easily? Can we use static method for retrieving database table? I have made static method of all method of service layer of DAO so Is this thread-safe?

like image 479
Kamlesh Kanazariya Avatar asked Jan 16 '15 07:01

Kamlesh Kanazariya


1 Answers

Spring Aspect Concept

Elaborating on my comment:

"Static methods cannot be cached. The way aspects work in Spring is by adding a wrapper class (a proxy) to the annotated class. There is no way in Java to add a wrapper to a static method."

Since Spring needs an object to wrap around in order to intercept calls to that object and perform various operations before delegating the modified input to the original object (thus spring aspects are possible).

Since anything that is static cannot be instantiated as an object, Spring has no way to wrap around it and intercept its calls (at least with the current implementation of Spring)

like image 189
Ceiling Gecko Avatar answered Oct 26 '22 16:10

Ceiling Gecko