Spring 使用注解配置使用ehcache

阅读更多

使用ehcache-spring-annotations使得在工程中简单配置即可使用缓存
下载地址:http://code.google.com/p/ehcache-spring-annotations/

 

需要的jar包,首先需要的是我们之前做SpringMVC时的各个Spring的jar包
然后需要把ehcache-spring-annotations-1.2.0文件夹内lib内的,非spring的jar加进去,因为我们已经增加了我们版本的spring
然后还需要动态代理的cglib包

 

SpringMVC的搭建就不再说了,参考
Spring MVC 入门示例 http://cuisuqiang.iteye.com/blog/2042931
Spring MVC Controller配置方式 http://cuisuqiang.iteye.com/blog/2043697

 

在spring主配置文件中配置ehcache注解的使用:



	
	
		
	
	

 

配置缓存配置文件ehcache.xml,改文件放在SRC下:



	
	
	

 

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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
// http://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做不同的操作,然后分别访问以下几个路径,为了方便看效果和学习,我把工程代码放到了附件
第一次没有缓存
http://localhost:8080/spring/hello.do?key=1&code=java
读取缓存
http://localhost:8080/spring/hello.do?key=1&code=java
更新缓存
http://localhost:8080/spring/hello.do?key=2&code=java
读取最新缓存
http://localhost:8080/spring/hello.do?key=1&code=java
情况缓存
http://localhost:8080/spring/hello.do?key=3

 

请您到ITEYE网站看 java小强 原创,谢谢!
http://cuisuqiang.iteye.com/ !

自建博客地址:http://www.javacui.com/ ,内容与ITEYE同步!

  • spring.zip (6.3 MB)
  • 下载次数: 339

你可能感兴趣的:(spring,ehcache,缓存,注解)