出現問題的原因:Spring cache的實現原理是基於AOP的動態代理實現的:即都在方法調用前後去獲取方法的名稱、參數、返回值,然後根據方法名稱、參數生成緩存的key(自定義的key例外),進行緩存。this調用不是代理對象的調用, 所以aop失效,註解失效。
解決辦法就是,我們獲取當前Bean,由它來調用。
SpringContextUtil
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; /** *不依賴於Autowired而使用java代碼快速獲取到spring管理的bean */ @Component @Lazy(false) public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextUtil .applicationContext = applicationContext; } public static ApplicationContext getApplicationContext() { return applicationContext; } public static Object getBean(String name) { return getApplicationContext().getBean(name); } public static <T> T getBean(Class<T> clazz) { return getApplicationContext().getBean(clazz); } public static <T> T getBean(String name, Class<T> clazz) { return getApplicationContext().getBean(name, clazz); } }
Service 調用
package com.mark.pay.service; import com.mark.pay.bean.User; import com.mark.pay.common.spring.SpringContextUtil; import com.mark.pay.dao.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.*; import org.springframework.stereotype.Service; @Service @CacheConfig(cacheNames = "user",cacheManager = "cacheManager") public class TestService { @Autowired UserMapper userMapper; @Cacheable( key ="#id") public User query(Integer id) { return userMapper.selectByPrimaryKey(id); } public User testCacheQuery(Integer id){ TestService service = SpringContextUtil.getBean(TestService.class); return service.query(id); } }