@Cacheable在项目中的具体使用

①在applicationContext.xml中引入

<import resource="classpath:/config/applicationContext-ehcache.xml"/>

②配置applicationContext-ehcache.xml文件

<?xml version="1.0" encoding="UTF8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:oxm="http://www.springframework.org/schema/oxm" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:ehcache="http://www.springframework.org/schema/cache"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
       http://www.springframework.org/schema/context   
       http://www.springframework.org/schema/context/spring-context-3.0.xsd    
       http://www.springframework.org/schema/oxm   
       http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd    
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    
       http://www.springframework.org/schema/cache  
       http://www.springframework.org/schema/cache/spring-cache.xsd">  
  
    <!-- 支持缓存注解 cache-manager默认为cacheManager,如果定义bean的id为cacheManeger,此处可以省略 -->    
    <ehcache:annotation-driven cache-manager="cacheManager" />  
  
    <!--  缓存工厂,指定ehcache.xml位置-->  
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">      
        <property name="configLocation">
            <value>classpath:/config/ehcache.xml</value>
        </property>   
    </bean>     
        
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">      
        <property name="cacheManager"  ref="cacheManagerFactory"/>      
    </bean>      
</beans>  

③配置ehcache.xml文件

<?xml version="1.0" encoding="UTF8"?>  
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">  
    <defaultCache eternal="true" maxElementsInMemory="100" overflowToDisk="false" />  
    <!-- 缓存配置-->  
    <cache name="queryCache" maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="900" timeToLiveSeconds="1800" overflowToDisk="true" />  
    <cache name="deleteCache" maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="900" timeToLiveSeconds="1800" overflowToDisk="true" /> 
    <cache name="menuCache" maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="900" timeToLiveSeconds="1800" overflowToDisk="true" /> 
    <cache name="indexPageCache" maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="900" timeToLiveSeconds="1800" overflowToDisk="true" />
    <cache name="newsCache" maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="900" timeToLiveSeconds="1800" overflowToDisk="true" />
</ehcache>

猜你喜欢

转载自www.cnblogs.com/java-ssh/p/12802907.html