Spring注解配置ehcache的方法有哪些?
- 内容介绍
- 文章标签
- 相关推荐
本文共计995个文字,预计阅读时间需要4分钟。
使用ehcache-spring-annotations可简化Spring项目中缓存配置,快速实现缓存功能。具体步骤如下:
1. 下载地址:[ehcache-spring-annotations](http://code.google.com/p/ehcache-spring-annotations/)
2.需要的jar包:
- SpringMVC项目中的所有Spring相关jar包 - ehcache-spring-annotations3. 首先配置ehcache.xml文件,定义缓存策略:
xml
4. 在Spring配置文件中添加ehcache.xml路径:
xml
5. 在需要使用缓存的类或方法上添加`@Cacheable`、`@CachePut`或`@CacheEvict`注解:
java@Cacheable(value=exampleCache, key=#id)public User getUserById(Long id) { // ...}
以上步骤即可实现Spring项目中ehcache缓存的配置和使用。
使用ehcache-spring-annotations使得在工程中简单配置即可使用缓存
下载地址:code.google.com/p/ehcache-spring-annotations/
需要的jar包,首先需要的是我们之前做SpringMVC时的各个Spring的jar包
然后需要把ehcache-spring-annotations-1.2.0文件夹内lib内的,非spring的jar加进去,因为我们已经增加了我们版本的spring
然后还需要动态代理的cglib包
在spring主配置文件中配置ehcache注解的使用:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:mvc="www.springframework.org/schema/mvc" xmlns:p="www.springframework.org/schema/p" xmlns:context="www.springframework.org/schema/context" xmlns:aop="www.springframework.org/schema/aop" xmlns:tx="www.springframework.org/schema/tx" xmlns:ehcache="ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans-3.0.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context-3.0.xsd www.springframework.org/schema/aop www.springframework.org/schema/aop/spring-aop-3.0.xsd www.springframework.org/schema/tx www.springframework.org/schema/tx/spring-tx-3.0.xsd www.springframework.org/schema/mvc www.springframework.org/schema/mvc/spring-mvc-3.0.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context-3.0.xsd ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring 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> <bean id="sacheService" class="test.CacheService"></bean> </beans>
配置缓存配置文件ehcache.xml,改文件放在SRC下:
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="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="testCache" eternal="false" maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" /> </ehcache>
CacheService是示例类,代码如下:
package test; import java.util.Date; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.googlecode.ehcache.annotations.Cacheable; import com.googlecode.ehcache.annotations.TriggersRemove; public class CacheService{ @SuppressWarnings("deprecation") @Cacheable(cacheName = "testCache") public String getName(String code){ System.out.println("查询编号:" + code); return new Date().toLocaleString() + "-->" + code; } @SuppressWarnings("deprecation") @Transactional(propagation = Propagation.REQUIRED) public String update(String code){ System.out.println("更新编号:" + code); return new Date().toLocaleString() + "-->" + code; } @TriggersRemove(cacheName="testCache",removeAll=true) public void flush(){ System.out.println("情况缓存"); System.out.println("Processing testFlushing"); } }
改类包含根据参数获取缓存值,更新缓存,情况缓存,都是使用注解标签实现。
Action类需要改动一下,代码如下:
package test; import javax.servlet.localhost:8080/spring/hello.do?key=1&code=java @org.springframework.stereotype.Controller public class HelloController{ private CacheService sacheService; @SuppressWarnings("deprecation") @RequestMapping("/hello.do") public String hello(HttpServletRequest request,HttpServletResponse response){ String key = request.getParameter("key"); if("1".equals(key)){ request.setAttribute("message", sacheService.getName(request.getParameter("code"))); }else if("2".equals(key)){ request.setAttribute("message", sacheService.update(request.getParameter("code"))); }else{ sacheService.flush(); request.setAttribute("message", sacheService.getName(request.getParameter("code"))); } return "hello"; } public CacheService getSacheService() { return sacheService; } @Autowired public void setSacheService(CacheService sacheService) { this.sacheService = sacheService; } }
根据key做不同的操作,然后分别访问以下几个路径,为了方便看效果和学习,我把工程代码放到了附件:
第一次没有缓存
localhost:8080/spring/hello.do?key=1&code=java
读取缓存
localhost:8080/spring/hello.do?key=1&code=java
更新缓存
localhost:8080/spring/hello.do?key=2&code=java
读取最新缓存
localhost:8080/spring/hello.do?key=1&code=java
情况缓存
localhost:8080/spring/hello.do?key=3
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计995个文字,预计阅读时间需要4分钟。
使用ehcache-spring-annotations可简化Spring项目中缓存配置,快速实现缓存功能。具体步骤如下:
1. 下载地址:[ehcache-spring-annotations](http://code.google.com/p/ehcache-spring-annotations/)
2.需要的jar包:
- SpringMVC项目中的所有Spring相关jar包 - ehcache-spring-annotations3. 首先配置ehcache.xml文件,定义缓存策略:
xml
4. 在Spring配置文件中添加ehcache.xml路径:
xml
5. 在需要使用缓存的类或方法上添加`@Cacheable`、`@CachePut`或`@CacheEvict`注解:
java@Cacheable(value=exampleCache, key=#id)public User getUserById(Long id) { // ...}
以上步骤即可实现Spring项目中ehcache缓存的配置和使用。
使用ehcache-spring-annotations使得在工程中简单配置即可使用缓存
下载地址:code.google.com/p/ehcache-spring-annotations/
需要的jar包,首先需要的是我们之前做SpringMVC时的各个Spring的jar包
然后需要把ehcache-spring-annotations-1.2.0文件夹内lib内的,非spring的jar加进去,因为我们已经增加了我们版本的spring
然后还需要动态代理的cglib包
在spring主配置文件中配置ehcache注解的使用:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:mvc="www.springframework.org/schema/mvc" xmlns:p="www.springframework.org/schema/p" xmlns:context="www.springframework.org/schema/context" xmlns:aop="www.springframework.org/schema/aop" xmlns:tx="www.springframework.org/schema/tx" xmlns:ehcache="ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans-3.0.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context-3.0.xsd www.springframework.org/schema/aop www.springframework.org/schema/aop/spring-aop-3.0.xsd www.springframework.org/schema/tx www.springframework.org/schema/tx/spring-tx-3.0.xsd www.springframework.org/schema/mvc www.springframework.org/schema/mvc/spring-mvc-3.0.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context-3.0.xsd ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring 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> <bean id="sacheService" class="test.CacheService"></bean> </beans>
配置缓存配置文件ehcache.xml,改文件放在SRC下:
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="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="testCache" eternal="false" maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" /> </ehcache>
CacheService是示例类,代码如下:
package test; import java.util.Date; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.googlecode.ehcache.annotations.Cacheable; import com.googlecode.ehcache.annotations.TriggersRemove; public class CacheService{ @SuppressWarnings("deprecation") @Cacheable(cacheName = "testCache") public String getName(String code){ System.out.println("查询编号:" + code); return new Date().toLocaleString() + "-->" + code; } @SuppressWarnings("deprecation") @Transactional(propagation = Propagation.REQUIRED) public String update(String code){ System.out.println("更新编号:" + code); return new Date().toLocaleString() + "-->" + code; } @TriggersRemove(cacheName="testCache",removeAll=true) public void flush(){ System.out.println("情况缓存"); System.out.println("Processing testFlushing"); } }
改类包含根据参数获取缓存值,更新缓存,情况缓存,都是使用注解标签实现。
Action类需要改动一下,代码如下:
package test; import javax.servlet.localhost:8080/spring/hello.do?key=1&code=java @org.springframework.stereotype.Controller public class HelloController{ private CacheService sacheService; @SuppressWarnings("deprecation") @RequestMapping("/hello.do") public String hello(HttpServletRequest request,HttpServletResponse response){ String key = request.getParameter("key"); if("1".equals(key)){ request.setAttribute("message", sacheService.getName(request.getParameter("code"))); }else if("2".equals(key)){ request.setAttribute("message", sacheService.update(request.getParameter("code"))); }else{ sacheService.flush(); request.setAttribute("message", sacheService.getName(request.getParameter("code"))); } return "hello"; } public CacheService getSacheService() { return sacheService; } @Autowired public void setSacheService(CacheService sacheService) { this.sacheService = sacheService; } }
根据key做不同的操作,然后分别访问以下几个路径,为了方便看效果和学习,我把工程代码放到了附件:
第一次没有缓存
localhost:8080/spring/hello.do?key=1&code=java
读取缓存
localhost:8080/spring/hello.do?key=1&code=java
更新缓存
localhost:8080/spring/hello.do?key=2&code=java
读取最新缓存
localhost:8080/spring/hello.do?key=1&code=java
情况缓存
localhost:8080/spring/hello.do?key=3
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

