spring4.x hibernate4.x 整合 ehcache 注解 annotate

废话不说 直接贴源码链接 :  https://git.oschina.net/alexgaoyh/alexgaoyh.git

使用ehcache来提高系统的性能,现在用的非常多, 也支持分布式的缓存,在hibernate当中作为二级缓存的实现产品,可以提高查询性能。

pom.xml

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
    <version>4.1.6.Final</version>
    </dependency>

在项目的src下面添加ehcache的配置文件ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
	<!--
    Subdirectories can be specified below the property e.g. java.io.tmpdir/one
    -->
    <diskStore path="java.io.tmpdir"/>

    <!--
    Mandatory Default Cache configuration. These settings will be applied to caches
    created programmtically using CacheManager.add(String cacheName)
    -->
    <defaultCache
	     maxElementsInMemory="10000"
	     eternal="false"
	     timeToIdleSeconds="120"
	     timeToLiveSeconds="120"
	     overflowToDisk="true"
	     maxElementsOnDisk="10000000"
	     diskPersistent="false"
	     diskExpiryThreadIntervalSeconds="120"
	     memoryStoreEvictionPolicy="LRU"
     />

    <cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
		   maxElementsInMemory="5000"
	       eternal="true"
	       overflowToDisk="true" />
	<cache name="org.hibernate.cache.internal.StandardQueryCache"
	       maxElementsInMemory="10000"
	       eternal="false"
	       timeToLiveSeconds="120"
	       overflowToDisk="true" />	

	<!--
	java文件注解查找cache方法名的策略:如果不指定java文件注解中的region="ehcache.xml中的name的属性值",
	则使用name名为com.lysoft.bean.user.User的cache(即类的全路径名称), 如果不存在与类名匹配的cache名称, 则用 defaultCache
	如果User包含set集合, 则需要另行指定其cache
	例如User包含citySet集合, 则也需要
	添加配置到ehcache.xml中
	-->
    <cache name="javaClassName" maxElementsInMemory="2000" eternal="false"
	       timeToIdleSeconds="120" timeToLiveSeconds="120"
	       overflowToDisk="true" />  

</ehcache>

在spring 集成hibernate 的配置文件中,添加如下配置

<!-- 开启查询缓存 -->
<prop key="hibernate.cache.use_query_cache">true</prop>
<!-- 开启二级缓存 -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<!-- 高速缓存提供程序 -->
<!-- 由于spring也使用了Ehcache, 保证双方都使用同一个缓存管理器 -->
<prop key="hibernate.cache.region.factory_class">
     org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
</prop>

Spring也使用ehcache, 所以也需要在spring配置文件中添加ehcache的配置

<!-- cacheManager, 指定ehcache.xml的位置 -->
	<bean id="cacheManagerEhcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
		<property name="configLocation">
        	<value>classpath:ehcache.xml</value>
        </property>
        <!-- 由于hibernate也使用了Ehcache, 保证双方都使用同一个缓存管理器 -->
        <property name="shared" value="true"/>
    </bean>

在类中定义:

@Entity
@Table(name = "t_user")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region="javaClassName")
public class User implements Serializable {  

}

默认情况下二级缓存只会对load get 之类的方法缓存, 想list iterator 之类的方法也使用缓存 必须跟查询缓存一起使用, 重写查询方法

.setCacheable(true)
criteria.setCacheable(true).list();

之后进行验证

时间: 2024-08-29 09:08:51

spring4.x hibernate4.x 整合 ehcache 注解 annotate的相关文章

Spring4 SpringMVC Hibernate4 Freemaker 整合例子

前话: 写了半年的Flex,也就是一个做一个WEB的视频监控浏览端,已经初步成型,现在是要做一个管理平台,于是终于又要用回JAVA了,但是一切都变的陌生了, 比如写个方法或者定义一个变量,总是会用Flex的语法去写,以前都是用Struts2 Hibernate Spring框架的,现在据说springMVC更流行了,于是花了这一周时间入了下门,感觉 确实不错,我是看的这个系列教程,跟开涛学SpringMvc http://jinnianshilongnian.iteye.com/category

struts-2.3+spring-4.0+hibernate-4.0整合项目

1.加入spring 1)加入jar包 2)配置web.xml applicationContext.xml (监听器) 3)加入spring的配置文件:aop context tx bean 1.加入hibernate 1.1建立持久化类,和其对应的xxx,hbm.xml文件,生产对应的数据表 1.2spring 整合hibernate 1)加入jar包 2)类路径下加入hibernate.cfg.xml文件.其中配置hibernate的基本配置 3)建立持久化类和对应的xxx.hbm.xml

spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除

写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天主题有所感触.不多说了,开干! 注:引入jar <!-- 引入ehcache缓存 --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.8.3</version&g

Spring4 MVC+Hibernate4+MySQL+Maven使用注解集成实例

在本教程中,我们将使用基于注解的配置集成Spring和Hibernate. 我们将开发包含表单要求用户输入一个简单的CRUD为导向Web应用程序,使用Hibernate保存输入的数据到 MySQL 数据库,从数据库和更新检索记录或删除它们在事务中,全部采用注解配置. 使用以下技术: Spring 4.0.6.RELEASE Hibernate Core 4.3.6.Final validation-api 1.1.0.Final hibernate-validator 5.1.3.Final M

Struts2+Spring4.2+Hibernate4.3整合

一.导包 antlr-2.7.7.jarasm-3.3.jarasm-commons-3.3.jarasm-tree-3.3.jarcom.springsource.com.mchange.v2.c3p0-0.9.1.2.jarcom.springsource.org.aopalliance-1.0.0.jarcom.springsource.org.apache.commons.logging-1.1.1.jarcom.springsource.org.apache.log4j-1.2.15.

Spring整合Ehcache管理缓存

Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它支持注解方式使用缓存,非常方便.本文先通过Ehcache独立应用的范例来介绍它的基本使用方法,然后再介绍与Spring整合的方法. 概述 Ehcache是什么?EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点.它是Hibernate中的默认缓存框架.Ehcache已经发布了3.1版本

ehcache注解使用问题常见总结

1. 支持缓存的方法最好有传入参数和返回数据 1.1 没有入参将无法自定义key,即无法保证数据更新时实时更新缓存中对应的数据(如果数据不会被改变则忽略)1.2 没有返回数据的话,当从缓存中获取的数据时,如法获取到数据 2. 不要在类的内部调用支持缓存的方法 2.1 对象内部调用支持缓存的方法是不会触发缓存功能的,因为ehcache要使用代理才可以缓存 3. 使用@CachePut注解时属性key和返回数据类型要一致 3.1 对应的@Cacheable和@CachePut,属性的key要保持一致

Spring4+MVC+Hibernate4全注解环境搭建(一)

声明: 以下任何观点.理解,都有可能是错的,那仅代表作者在某一时刻结合自己的学习经历和思考得出的观点,很明显,这样的前提下很多都可能是错的.请各位在看到任何可疑观点时,都不要轻信,如果你们在喷我的时候能把理由一并说出来,那我就非常感激了.像什么“你懂的”,“当然是!不然还能是什么.”那样的话恐怕既说服不了我,也说服不了别人. 目前为止我对这几个框架认识: 我的理解不一定对,但是我还是在此首先明确一下我为什么选择的是Spring4+MVC+Hibernate4. Spring就是用来提供一个IoC

spring4和hibernate4.0.0的整合

1.在myeclipse下面创建一个java工程或者web工程,我创建的时web工程,用的myeclipse2013 2.导入spring的依赖包 3.导入hibernate的依赖包 4.在src目录下面配置hibernate的核心文件hibernate.cfg.xml <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate C