博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
缓存初解(五)---SpringMVC基于注解的缓存配置--web应用实例
阅读量:4309 次
发布时间:2019-06-06

本文共 3779 字,大约阅读时间需要 12 分钟。

之前为大家介绍了如何使用spring注解来进行缓存配置 (EHCache 和 OSCache)的简单的例子,详见

现在介绍一下如何在基于注解springMVC的web应用中使用注解缓存,其实很简单,就是将springMVC配置文件与缓存注解文件一起声明到context中就OK了。

下面我就来构建一个基于spring注解小型的web应用,这里我使用EHCache来作为缓存方案

jar依赖:

ehcache-core-1.7.2.jar

jakarta-oro-2.0.8.jar
slf4j-api-1.5.8.jar
slf4j-jdk14-1.5.8.jar 
cglib-nodep-2.1_3.jar
commons-logging.jar
log4j-1.2.15.jar
spring-modules-cache.jar
spring.jar 
jstl.jar

standard.jar

接着我们来编写web.xml

SpringCacheWeb
log4jConfigLocation
classpath:log4j.properties
contextConfigLocation
/WEB-INF/spring-servlet.xml
Set Character Encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
Set Character Encoding
*.do
org.springframework.web.util.Log4jConfigListener
org.springframework.web.context.ContextLoaderListener
spring
org.springframework.web.servlet.DispatcherServlet
1
spring
*.do
10
index.jsp
index.html
index.htm

 接着我们来编写spring-servlet.xml

 

 

ok,我们接着编写applicationContext-ehcache.xml,还记得之前介绍的基于命名空间的配置吗,如下:

 ehcache.xml如下

 

ok,配置文件都完成了,接着我们来编写controller、service和dao

1.CacheDemoController:

package com.netqin.function.cacheDemo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class CacheDemoController {	@Autowired	private CacheDemoService service;	@RequestMapping("/demo.do")	public String handleIndex(Model model) {		System.out.println(service.getName(0));		model.addAttribute("name", service.getName(0));		return "cacheDemo";	}	@RequestMapping("/demoFulsh.do") 	public String handleFulsh(Model model) {  		service.flush();  		return "cacheDemo";	 }}

 2.CacheDemoService :

package com.netqin.function.cacheDemo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springmodules.cache.annotations.CacheFlush;import org.springmodules.cache.annotations.Cacheable;@Servicepublic class CacheDemoService {	@Autowired	private CacheDemoDao dao;		@Cacheable(modelId = "testCaching")	public String getName(int id){		System.out.println("Processing testCaching");		return dao.getName(id);	}		@CacheFlush(modelId = "testFlushing")	public void flush(){		System.out.println("Processing testFlushing");	}}

 

我们只对service层加入了注解缓存配置。

 

接着我们来写一个简单的页面,cacheDemo.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
Insert title hereCacheDemo:${name}

 

ok,一切就绪,我们启动服务器,并访问

 

多请求几次,请求两次的输出结果:

Processing testCaching

NameId:0
NameId:0

 

说明缓存起作用了,ok!

 

接着我们刷新该缓存,访问

再请求

输出结果:

Processing testCaching

NameId:0
NameId:0
Processing testFlushing
Processing testCaching
NameId:0

 

缓存刷新成功。

转载于:https://www.cnblogs.com/wcyBlog/p/3949709.html

你可能感兴趣的文章
iOS开发的一些奇巧淫技
查看>>
常浏览的博客和网站
查看>>
Xcode 工程文件打开不出来, cannot be opened because the project file cannot be parsed.
查看>>
iOS在Xcode6中怎么创建OC category文件
查看>>
5、JavaWeb学习之基础篇—标签(自定义&JSTL)
查看>>
8、JavaWEB学习之基础篇—文件上传&下载
查看>>
reRender属性的使用
查看>>
href="javascript:void(0)"
查看>>
h:panelGrid、h:panelGroup标签学习
查看>>
f:facet标签 的用法
查看>>
<h:panelgroup>相当于span元素
查看>>
java中append()的方法
查看>>
必学高级SQL语句
查看>>
经典SQL语句大全
查看>>
log日志记录是什么
查看>>
<rich:modelPanel>标签的使用
查看>>
<h:commandLink>和<h:inputLink>的区别
查看>>
<a4j:keeyAlive>的英文介绍
查看>>
关于list对象的转化问题
查看>>
VOPO对象介绍
查看>>