I have writewritten an article about how to cache JSF beans getter with Spring AOP.
I create a simple MethodInterceptorMethodInterceptor which intercepts all methods anotatedannotated with a special anotationannotation:
public class CacheAdvice implements MethodInterceptor { private static Logger logger = LoggerFactory.getLogger(CacheAdvice.class); @Autowired private CacheService cacheService; @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { String key = methodInvocation.getThis() + methodInvocation.getMethod().getName(); String thread = Thread.currentThread().getName(); Object cachedValue = cacheService.getData(thread , key); if (cachedValue == null){ cachedValue = methodInvocation.proceed(); cacheService.cacheData(thread , key , cachedValue); logger.debug("Cache miss " + thread + " " + key); } else{ logger.debug("Cached hit " + thread + " " + key); } return cachedValue; } public CacheService getCacheService() { return cacheService; } public void setCacheService(CacheService cacheService) { this.cacheService = cacheService; } } }
This interceptor is used in a spring configuration file :
<bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="pointcut"> <bean class="org.springframework.aop.support.annotation.AnnotationMatchingPointcut"> <constructor-arg index="0" name="classAnnotationType" type="java.lang.Class"> <null/> </constructor-arg> <constructor-arg index="1" value="com._4dconcept.docAdvance.jsfCache.annotation.Cacheable" name="methodAnnotationType" type="java.lang.Class"/> </bean> </property> <property name="advice"> <bean class="com._4dconcept.docAdvance.jsfCache.CacheAdvice"/> </property> </bean> Hope it will help !