要加cacheNames 畏鼓;不然報(bào)錯(cuò):
java.lang.IllegalStateException: No cache could be resolved for 'Builder[public abstract java.util.List com.test.findByXXX()] caches=[] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'' using resolver 'org.springframework.cache.interceptor.SimpleCacheResolver@12fddded'. At least one cache should be provided per cache operation.
解決辦法:
@Cacheable(cacheNames = {"user","user"},key = "#id")
推測(cè):
總之就是接口上的注解或者方法上的注解至少要有一個(gè)給出 cacheNames屬性, 具體作用暫時(shí)沒(méi)有深究, 另外附上一個(gè)網(wǎng)上的ehcache.xml配置文件, 我把文件內(nèi)容全部注釋掉了, 緩存也是可以生效的
目測(cè)是根據(jù) cacheNames的值 與 ehcache.xml配置文件里的<cache name="">屬性進(jìn)行匹配對(duì)應(yīng)
<ehcache> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <cache name="weather" maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="18000" timeToLiveSeconds="36000" overflowToDisk="false" /> </ehcache>