SpringMvc整合ehcaech

1、web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>fbd-web</display-name>
    <welcome-file-list>
        <welcome-file>/common/index.html</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:fbd/*/spring/applicationContext*.xml</param-value>
    </context-param>

    <!-- struts config begin -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:fbd/core/spring/springmvc-servlet.xml,
                classpath:fbd/web/spring/springmvc-servlet.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    <!-- struts config end -->
    <filter>
        <filter-name>UrlRewriteFilter</filter-name>
        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>UrlRewriteFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>

    <!-- dbcp config begin -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- spring encoding -->
    <filter>
        <filter-name>Spring character encoding filter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>Spring character encoding filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- spring log -->
    <!-- spring config end -->
    <mime-mapping>
        <extension>doc</extension>
        <mime-type>application/msword</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>xls</extension>
        <mime-type>application/msexcel</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>pdf</extension>
        <mime-type>application/pdf</mime-type>
    </mime-mapping>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>

2、applicationContext-config.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/tx   
            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">


<!-- 启用缓存注解功能(请将其配置在Spring主配置文件中) -->
    <cache:annotation-driven cache-manager="cacheManager" />
    <!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3.1开始提供的) -->
    <!-- <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> 
        <property name="caches"> <set> <bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/> 
        </set> </property> </bean> -->
    <!-- 若只想使用Spring自身提供的缓存器,则注释掉下面的两个关于Ehcache配置的bean,并启用上面的SimpleCacheManager即可 -->
    <!-- Spring提供的基于的Ehcache实现的缓存管理器 -->
    <bean id="cacheManagerFactory"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml" />
    </bean>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="cacheManagerFactory" />
    </bean>
</beans>

3、applicationContext-ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>  
<!-- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"  
    updateCheck="false">   -->
    <!--  
        name:cache唯一标识   
        eternal:缓存是否永久有效   
        maxElementsInMemory:内存中最大缓存对象数  
        overflowToDisk(true,false):缓存对象达到最大数后,将缓存写到硬盘中  
        diskPersistent:硬盘持久化  
        timeToIdleSeconds:缓存清除时间   
        timeToLiveSeconds:缓存存活时间  
        memoryStoreEvictionPolicy:缓存清空策略  
        1.FIFO:first in first out 先讲先出  
        2.LFU: Less Frequently Used 一直以来最少被使用的  
        3.LRU:Least Recently Used  最近最少使用的   
    -->  
<!--     <defaultCache maxElementsInMemory="1000" eternal="false"  
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" />


    <cache name="infoCache" eternal="false" maxElementsInMemory="1000"  
        overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="3600"  
        timeToLiveSeconds="3600" memoryStoreEvictionPolicy="LFU" />  
</ehcache> -->


<!-- Ehcache2.x的变化(取自https://github.com/springside/springside4/wiki/Ehcache) -->  
<!-- 1)最好在ehcache.xml中声明不进行updateCheck -->  
<!-- 2)为了配合BigMemory和Size Limit,原来的属性最好改名 -->  
<!--   maxElementsInMemory->maxEntriesLocalHeap -->  
<!--   maxElementsOnDisk->maxEntriesLocalDisk -->  
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"  
    updateCheck="false">  
    <diskStore path="java.io.tmpdir"/>  
    <defaultCache  
           maxElementsInMemory="1000"  
           eternal="false"  
           timeToIdleSeconds="120"  
           timeToLiveSeconds="120"  
           overflowToDisk="false"/>  
    <cache name="userCache"  
           maxElementsOnDisk="20000"  
           maxElementsInMemory="2000"  
           eternal="true"  
           overflowToDisk="true"  
           diskPersistent="true"/>  
</ehcache> 
<!--  
<diskStore>==========当内存缓存中对象数量超过maxElementsInMemory时,将缓存对象写到磁盘缓存中(需对象实现序列化接口)  
<diskStore path="">==用来配置磁盘缓存使用的物理路径,Ehcache磁盘缓存使用的文件后缀名是*.data和*.index  
name=================缓存名称,cache的唯一标识(ehcache会把这个cache放到HashMap里)  
maxElementsOnDisk====磁盘缓存中最多可以存放的元素数量,0表示无穷大  
maxElementsInMemory==内存缓存中最多可以存放的元素数量,若放入Cache中的元素超过这个数值,则有以下两种情况  
                     1)若overflowToDisk=true,则会将Cache中多出的元素放入磁盘文件中  
                     2)若overflowToDisk=false,则根据memoryStoreEvictionPolicy策略替换Cache中原有的元素  
eternal==============缓存中对象是否永久有效,即是否永驻内存,true时将忽略timeToIdleSeconds和timeToLiveSeconds  
timeToIdleSeconds====缓存数据在失效前的允许闲置时间(单位:秒),仅当eternal=false时使用,默认值是0表示可闲置时间无穷大,此为可选属性  
                     即访问这个cache中元素的最大间隔时间,若超过这个时间没有访问此Cache中的某个元素,那么此元素将被从Cache中清除  
timeToLiveSeconds====缓存数据在失效前的允许存活时间(单位:秒),仅当eternal=false时使用,默认值是0表示可存活时间无穷大  
                     即Cache中的某元素从创建到清楚的生存时间,也就是说从创建开始计时,当超过这个时间时,此元素将从Cache中清除  
overflowToDisk=======内存不足时,是否启用磁盘缓存(即内存中对象数量达到maxElementsInMemory时,Ehcache会将对象写到磁盘中)  
                     会根据标签中path值查找对应的属性值,写入磁盘的文件会放在path文件夹下,文件的名称是cache的名称,后缀名是data  
diskPersistent=======是否持久化磁盘缓存,当这个属性的值为true时,系统在初始化时会在磁盘中查找文件名为cache名称,后缀名为index的文件  
                     这个文件中存放了已经持久化在磁盘中的cache的index,找到后会把cache加载到内存  
                     要想把cache真正持久化到磁盘,写程序时注意执行net.sf.ehcache.Cache.put(Element element)后要调用flush()方法  
diskExpiryThreadIntervalSeconds==磁盘缓存的清理线程运行间隔,默认是120秒  
diskSpoolBufferSizeMB============设置DiskStore(磁盘缓存)的缓存区大小,默认是30MB  
memoryStoreEvictionPolicy========内存存储与释放策略,即达到maxElementsInMemory限制时,Ehcache会根据指定策略清理内存  
                                 共有三种策略,分别为LRU(最近最少使用)、LFU(最常用的)、FIFO(先进先出)  
-->

4、测试代码

package com.fbd.core.app.ehcache.dao.impl;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fbd.core.app.ehcache.dao.IEhcacheDao;

@Repository(value="ehcacheDao")
public class EhcacheDaoImpl implements IEhcacheDao {


    /*@Autowired
    private CacheManager cacheManager;


    public void set(String key,String value){
        System.out.println("============================");
        this.cacheManager.getCache("userCache").put(new Element(key,value));
        System.out.println("============================");
    }

    public Element get(String key){
        return this.cacheManager.getCache("userCache").get(key);
    }

    public String getValue(String key){
        Element element = this.cacheManager.getCache("userCache").get(key);
        if(element!=null){
            return element.getValue().toString();
        }
        return "";
    }

    @Override
    public void set(String key, Object object) {

    }*/
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值