首页 > 其他分享 >Spring 3.2.1.RELEASE MVC 基于注解ehcache.xml 配置方式

Spring 3.2.1.RELEASE MVC 基于注解ehcache.xml 配置方式

时间:2023-03-21 11:31:48浏览次数:45  
标签:ehcache xml 缓存 http spring springframework Spring org


载的关联包里的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>



[color=red][b]Spring 3.0.5 MVC 基于注解ehcache.xml 配置方式 [/b][/color][url]http://www.blogjava.net/zzzlyr/articles/343234.html[/url]


spring 3.0.5 发布后,公司使用Spring MVC +Hibernate 3.5 做项目,其中用到了缓存机制,spring 3.0.5 中ehcache配置方法很简单,其中缓存机制很细颗粒化,可以具体到把每个方式的返回值做缓存,好了不说废话下面开始:



需要JAR包:


第一:spring 3.0.5 其中JAR;


第二:另外需要增量JAR包(cglib-2.2.jar,ehcache-spring-annotations-1.1.2.jar)注意版本;



其中applicationContext.xml 其中配置:


<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" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>




加到你的文件中去,上边是头信息自己可以比照下,没有的加进去;




在你的 src 目录下新建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">
<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>



其中里边详细配置自己可以去上网搜下;



这样基本上配置完成了:



DAO层缓存:例如下边这个方法的返回值需要缓存:



@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);
}



[b]@Cacheable(cacheName="departCache")[/b] 加上这句话,其中cacheName 对应ehcache.xml 中的<cache name="departCache"



这样这个方法返回值就可以被缓存起来的了,但是怎么样把缓存数据和数据库中的数据实现同步呢?



如果对这个PO做update ,save,delete 可以实现这样策略如下:



@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;
}



好了到此配置完毕,但是更加详细缓存配置策略需要研究(例如:当update数据时候,不全部清掉缓存,就可以达到与数据库同步效果)


[url]以下配置经本人完成测试通过(只限于本版本)。


​http://luck332.iteye.com/blog/1001783[/url] ​​编写测试类


package com.dzf.cache;



import org.springframework.stereotype.Service;


import org.springframework.transaction.annotation.Transactional;



import com.googlecode.ehcache.annotations.Cacheable;


import com.googlecode.ehcache.annotations.TriggersRemove;


@Service


@Transactional


public class CacheService{



@Cacheable(cacheName = "testCache")


public String getName(int i){


System.out.println("Processing testCache");


return "nihao:"+i;


}



@TriggersRemove(cacheName="testCache")


public void flush(){


System.out.println("Processing testFlushing");


}


}


很容易就把cache给管理起来了。相当的方便。




碰到问题:当在Hibernate也使用缓存的时候


<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:packagesToScan="com.pandy.ssh4.domian">
<property name="dataSource">
<ref bean="dataSource" />
</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>


会出现[color=red][b]Another unnamed CacheManager already exists in the same VM[/b][/color]错误,又如何解决呢?


[color=red][b]有必要在spring和hibernate都配置ehcache缓存么?[/b][/color]


一个是针对数据库缓存,一个是针对业务数据缓存.

标签:ehcache,xml,缓存,http,spring,springframework,Spring,org
From: https://blog.51cto.com/u_3871599/6139972

相关文章

  • 【SpringBoot】自定义注解+拦截
     创建一个注解,用来校验身份@Target({ElementType.TYPE,ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)public@interfaceAuthUser{//intuser();......
  • springCloud-复习
    Mybatis-plus依赖:1是mybatis-plus-boot-starter本身的依赖,2是mysql-connector-java驱动,3是druid-spring-boot-starter连接池,4是mybatis-plus-generator生成器。父项目......
  • Spring的手动注入,动态注入
    参数说明:Objectbean,就是一个已经被创建的实例;ApplicationContext:Spring的Context;AutowireCapableBeanFactory.AUTOWIRE_BY_NAME:按照名字来注......
  • SpringBoot——包扫描@ComponentScan源码分析
    摘要博文参考(1)选择SpringCloud作为微服务架构的原因(2)SpringBoot和SpirngCloud,请你谈谈对他们的理解(3)什么是服务熔断?什么是服务降级?(4)微服务的优缺点分别是什么?(5)你所知道......
  • Spring自动创建实例
    动态的调用无参构造方法来创建实例.Classclazz=ClassUtils.forName(className);executor=(ReportExecutor)BeanUtils.instantiateClass(clazz)......
  • SpringBoot——springboot自动配置原理
    摘要 主要是介绍的Springboot的底层原理。web.xml配置Spring环境<!--Spring监听器--><1istener>org.springframework.web.context.ContextLoaderListener</1istener-......
  • SpringBoot——spring quarter原理和应用
    摘要1、JDK定时器timer使用及原理分析2、定时任务线程池解析3、定时任务框架-quarter小顶堆......
  • SpringCloud——SpringCloud Alibaba Seata 原理与实战
    摘要主要讲解是分布式事务指事务的操作位于不同的节点上,需要保证事务的AICD特性。分布式事务顾名思义就是要在分布式系统中实现事务,它其实是由多个本地事务组合而成。对于......
  • SpringCloud——springcloud大厂面试问题
    摘要 毫无疑问,SpringCloud是目前微服务架构领域的翘楚,无数的书籍博客都在讲解这个技术。不过大多数讲解还停留在对SpringCloud功能使用的层面,其底层的很多原理,很多人可......
  • 5-springboot集成热部署的方式
    热部署是指当我们修改代码后,服务能自动重启加载新修改的内容,这样大大提高了我们开发的效率;Springboot热部署通过添加一个插件实现;插件为:spring-boot-devtools,在Maven中......