`
孤星119
  • 浏览: 122824 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring+Ehcache xml文件配置方式

 
阅读更多

Srping+Ehcache 在Service层配置缓存(太懒了,不写如何配置了,就代码吧)

 

1. ehcache 文件

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">

    <diskStore path="java.io.tmpdir"/>
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="520"
            timeToLiveSeconds="520"
            overflowToDisk="true"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
     />

	 
	<cache name="testServiceCache"
            maxElementsInMemory="10"
            eternal="false"
            timeToIdleSeconds="200"
            timeToLiveSeconds="300"
            overflowToDisk="true"
            />    
            
</ehcache>

 

2. applicationContext.xml 文件 相关代码

    <!-- Service Cache   Ehcache -->
       
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    	<property name="configLocation">  
        	<value>ehcache.xml</value>  
      	</property> 
    </bean>

    <bean id="methodCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
        <property name="cacheManager">
            <ref local="cacheManager"/>
        </property>
        <property name="cacheName">
            <value>testServiceCache</value>
        </property>
    </bean>
    
	<!-- methodCacheInterceptor 要自己实现的代码 -->
    <bean id="methodCacheInterceptor" class="com.miao.service.MethodCacheInterceptor">
        <property name="cache">
            <ref local="methodCache"/>
        </property>
    </bean>
    
    <!-- 配置要拦截的service, 拦截service包下所有类的所有方法  -->
    <aop:config>
		<aop:pointcut id="methodCachePointCut" expression="execution(* com.miao.service.*.*(..))" />
		<aop:advisor pointcut-ref="methodCachePointCut" advice-ref="methodCacheInterceptor" />
	</aop:config>
    
	<!--  也可用此种方式 指定哪些方法使用cache-->
    <!-- 
    <bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice">
            <ref local="methodCacheInterceptor"/>
        </property>
        <property name="patterns">
            <list>
                <value>.*getStepPartKitList</value>
            </list>
        </property>
    </bean>
	-->

 

3.MethodCacheInterceptor

 

public class MethodCacheInterceptor implements MethodInterceptor,InitializingBean {
	private static final Log LOGGER = LogFactory.getLog(MethodCacheInterceptor.class);
	
	private Cache cache;

	public void setCache(Cache cache) {
		this.cache = cache;
	}
	

	public Object invoke(MethodInvocation invocation) throws Throwable {
	
		String targetName = invocation.getThis().getClass().getName();
		String methodName = invocation.getMethod().getName();
		Object[] arguments = invocation.getArguments();
		Object result;

		String cacheKey = getCacheKey(targetName, methodName, arguments);
		Element element = cache.get(cacheKey);
		if (element == null) {
			result = invocation.proceed();   //调用被拦截的目标方法
			LOGGER.info("Set into Cache");
			element = new Element(cacheKey, (Serializable) result);
			cache.put(element);
		}
		return element.getValue();
	}

	
	private String getCacheKey(String targetName, String methodName,Object[] arguments) {
		StringBuffer sb = new StringBuffer();	
		//拼cacheKey 这里使用 className.methodName(arg1,arg2.....argN) 作为key
		sb.append(targetName).append(".").append(methodName).append("(");
		if ((arguments != null) && (arguments.length != 0)) {
			for (int i = 0; i < arguments.length; i++) {
				sb.append(arguments[i]);
				if (i + 1 != arguments.length) {
                    sb.append(",");
                }
			}
		}
		sb.append(")");
		return sb.toString();
	}

	public void afterPropertiesSet() throws Exception {
		if(null == cache) {
			throw new IllegalArgumentException("Cache should not be null.");
		}
	}

}

 

 4.使用Junit测试即可,传递相同的参数,调用service内的同一个方法,只会进入DAO一次

分享到:
评论

相关推荐

    Struts2+Spring+Hibernate+Ehcache+AJAX+JQuery+Oracle 框架集成用户登录注册Demo工程

    1.通过google ehcache-spring-annotatios.jar自动注解方式实现整合Spring+Ehcache。 2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。...

    spring+shiro+ehcache例子

    在web.xml中配置log4j信息打印 (需要自己将log4j的配置文件给打开) 三: 配置文件 查看/src/config/ ,配置文件可观察文件名称理解 四: 登录名为2:可以进行权限的验证,以及shiro的缓存。 登录名为任意...

    Spring+MyBatis/Hibernate+Ehcache+Maven的Demo Web项目(稳定版)

    1.标题所提及技术的整合,Spring包括mvc、aop、ioc等。个人属于强迫症类型,技术水平怎么样再说,代码必须好看 2.Hibernate几个级别缓存对比。见DaoImpl类 3.Ehcache方法缓存及页面缓存。见applicationContext-cache...

    Spring+MyBatis/Hibernate+Ehcache+Maven的Demo Web项目

    1.标题所提及技术的整合,Spring包括mvc、aop、ioc等。个人属于强迫症类型,技术水平怎么样再说,代码必须好看 2.Hibernate几个级别缓存对比。见DaoImpl类 3.Ehcache方法缓存及页面缓存。见applicationContext-cache...

    spring+spring mvc+hibernate开发工程财务管理辅助系统

    项目描述 辅助业务人员对工程财务状况...2.spring中关于DataSource的配置,需要在tomcat的conf/context.xml中添加数据源 3.PaymentServiceImpl.java中有个import错误 4.FMS.sql数据库文件较大,导入时间较长,请耐心

    spring整合EhCache 基于注解的方式

    采用了java配置和xml配置两种方式。主要用于学习。 使用java配置时将SpringTestCase.java 文件中的@ContextConfiguration(locations = { "classpath:applicationContext.xml" }) 注释掉 使用xml配置时。将...

    springboot 整合ehcache+redis 通过配置文件切换缓存类型

    ehcache :添加依赖 pom.xml 2、添加配置文件ehcache.xml 3、添加注解@EnableCaching @Cacheable 4、插入缓存 5 读取缓存 6 设置缓存过期时间ehcache.xml --&gt;timeToLiveSeconds。 redis : 1、添加依赖 pom.xml 2、...

    spring整合EhCache 基于注解

    采用了java配置和xml配置两种方式。主要用于学习。 使用java配置时将SpringTestCase.java 文件中的@ContextConfiguration(locations = { "classpath:applicationContext.xml" }) 注释掉 使用xml配置时。将...

    Spring 3.0+Struts2+Mybatis 3 + p6spy 平台框架

    这是自己整合的Spring 3.0+Struts2+Mybatis 3 + p6spy +ehcache的平台框架,内含一点示例代码,目前ehcache没有使用。直接编译后发布就能用 测试环境基于JDK1.6+Tomcat 6.0. 大家拿到后请根据实际情况修改 ...

    简单配置 shiro + spring +springMVC+hibernate简单框架

    SpringMVC整合Shiro,Shiro是一个强大易用的Java安全框架,提供了...配置applicationContext-shiro.xml 1. 配置authorizingRealm 2.Shiro Filter 设置拦截的内容和登录页面和成功、失败页面 3.配置securityManager ...

    spring+struts+hibernate+dwr+jstl做的实例

    完全由Spring接管DWR AJAX处理(不需要配置dwr.xml)等技术点 源码在/mytest/WEB-INF/src下 Winxp sp3+JDK1.6+Tomcat 6.0下运行通过 因文件长度限制,未加入相关类库,请自行加入,需要的类库有: dom.jar...

    springMVC:MySQL+hibernate+thymeleaf+ehcache+C3P0 自定义多数据源

    springMVC 带有 MySQL+hibernate+thymeleaf 的 springMVC 框架 这是一个原始的 springMVC MAVEN 项目。 包含 MySQL 支持 Hibernate 百里香模板引擎 结构体 核 ** 框架(GenericDao,... 网页.xml 将要在JerseyServlet

    struts2.1.6+spring2.0+hibernate3.2常用配置包

    MyEclipse8.0中自带的struts2版本是2.1.6,spring版本有2.0,2.5的,hibernate版本较多些至3.2,首先选版本就选择最优的,struts2没的选只有2.1.6版的,所以先导入struts2支持,然后是spring选的是2.0,问题就出在...

    spring ehcache

    该项目使用了spring cache第三方插件ehcache ,是一个完整的项目其中涉及一张表,需要用户自己根据mybatis的配置xml自行在数据库中添加一张表,使用的是spring3 xml配置方式,希望对你们有帮助,有问题,我可以邮箱...

    基于JTT808协议的车辆监控系统架构方案

    1、网关应用采用mina+spring+ehcache框架,主要功能是接受终端的tcp/udp链接,解析终端的上行消息以及封装平台下发的下行消息,本网关应用已历经并通过多次交通部部标部标的检测,性能稳定;同时网关采用json消息与...

    NewsSystem:基于Struts + Spring + Hibernate + Bootstrap

    撑杆春天冬眠EhCache——缓存框架JSP前端技术: jQuery的引导程序UEditor——在线HTML编辑器模块介绍公共类设计Web.xml中配置Struts及初始化Spring容器,准备WEB-INF路径下applicationContex.xml文件作为Spring配置...

    cxf(jax-ws)+spring+hibernate整合包

    woodstox-core-asl-4.2.0.jar,wsdl4j-1.6.3.jar,wss4j-1.6.11.jar,xml-resolver-1.2.jar,xmlbeans-2.6.0.jar,xmlschema-core-2.0.3.jar,xmlsec-1.5.5.jar,xmltooling-1.3.2-1.jar,xsdlib-2010.1.jar

    最新版本的Struts2+Spring4+Hibernate4三大框架整合(改进:增加整合Quartz和Gson)

    2、 Spring配置提供了多种可选整合方案:详见applicationContext.xml的配置 (1) 提供支持三种数据源方案:C3P0、DBCP、JNDI,三选一,请注释或删除其他方案 (2) 提取四种Hibernate整合方案:四选一,请注释或...

    基于Spring的注册登录系统

    该项目基于Spring 3.1.3, Spring MVC, Hibernate 4.1.7, EhCache, Spring Data JPA 1.1.0, MySQL 5, Spring Security 3.1.3, Spring Mail, Recapcha等技术实现了网站注册、登录系统。项目包含了详尽的配置信息。 ...

    从零开始学Spring Boot

    1.34 Spring Boot导入XML配置 1.35 Spring Boot使用@SpringBootApplication注解 1.36 Spring Boot 监控和管理生产环境 1.37 Spring Boot的启动器Starter详解 1.38 Spring Boot集成Redis实现缓存机制 1.39 Spring ...

Global site tag (gtag.js) - Google Analytics