Spring 3.2.1.RELEASE MVC 基于注解ehcache.xml 配置方式
载的关联包里的ehcache-spring-annotations.jar之外, 还需要spring-context-support.jar, cblib-2.2.jar.
<dependency><groupId>com.googlecode.ehcache-spring-annotations</groupId><artifactId>ehcache-spring-annotations</artifactId><version>1.2.0</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>3.2.1.RELEASE</version></dependency>
<beans xmlns="http://www.springframework.org/schema/beans" 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" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd"><ehcache:annotation-driven cache-manager="ehCacheManager" /> <bean id="ehCacheManager" value="classpath:ehcache.xml" /> </bean>
<?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"> <diskStore path="java.io.tmpdir" /> <defaultCache eternal="false" maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" /> <cache name="departCache" eternal="false" maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" /></ehcache>
@SuppressWarnings("unchecked") //spring 3 基于注解ehcache缓存配置; @Cacheable(cacheName="departCache") public List<AppDepart> getChildDepart(Integer id) throws Exception { return this.getHibernateTemplate().find("from AppDepart where state=1 and idParent="+id); }
@Transactional(propagation = Propagation.REQUIRED) //设定spring的ecache缓存策略,当编辑机构时候,把缓存全部清除掉,以达到缓存那数据同步; @TriggersRemove(cacheName="departCache",removeAll=true) public boolean editDepart(String depno, String depName) { boolean flag = false; try { AppDepart depart = departDao.getAppdepart(depno); depart.setDepName(depName); departDao.update(depart); flag = true; } catch (Exception e) { e.printStackTrace(); } return flag; }
<bean id="sessionFactory"/></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop><prop key="hibernate.cache.use_query_cache">true</prop><prop key="hibernate.cache.use_second_level_cache">true</prop><prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop><prop key="hibernate.cache.use_structured_entries">true</prop><prop key="hibernate.generate_statistics">true</prop></props></property></bean>