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

Spring基于注解的缓存配置--EHCache AND 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

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
id="WebApp_ID" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>SpringCacheWeb</display-name> <!-- 由spring加载log4j -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param> <!-- 声明spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-servlet.xml
</param-value>
</context-param> <!-- 使用UTF-8编码 -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping> <!-- 负责初始化log4j-->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener> <!-- 负责初始化spring上下文-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- springMVC控制器-->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <session-config>
<session-timeout>10</session-timeout>
</session-config> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
</web-app>

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"
default-lazy-init="true"> <!--启用注解 定义组件查找规则 -->
<context:component-scan base-package="com.netqin">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository" />
</context:component-scan> <!-- 视图查找器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView">
</property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <!-- 加载ehcache缓存配置文件 说明:在这里我遇到了这样一个问题,当使用@Service等注解的方式将类声明到配置文件中时,
就需要将缓存配置import到主配置文件中,否则缓存会不起作用
如果是通过<bean>声明到配置文件中时,
则只需要在web.xml的contextConfigLocation中加入applicationContext-ehcache.xml即可,
不过还是推荐使用如下方式吧,因为这样不会有任何问题
-->
<import resource="classpath:applicationContext-ehcache.xml"/>
</beans>

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"> <ehcache:config configLocation="classpath:ehcache.xml"
id="cacheProvider" />
<ehcache:annotations providerId="cacheProvider">
<ehcache:caching cacheName="testCache" id="testCaching" />
<ehcache:flushing cacheNames="testCache" id="testFlushing" />
</ehcache:annotations> </beans>

ehcache.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect">
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<cache name="testCache"
maxElementsInMemory="10000"
maxElementsOnDisk="1000"
eternal="false"
overflowToDisk="true"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
/>
</ehcache>

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; @Controller
public 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; @Service
public 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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
CacheDemo:${name}
</body>
</html>

ok,一切就绪,我们启动服务器,并访问http://localhost:8080/cache/demo.do

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

Processing testCaching
NameId:0
NameId:0

说明缓存起作用了,ok!

接着我们刷新该缓存,访问http://localhost:8080/cache/demoFulsh.do

再请求http://localhost:8080/cache/demo.do

输出结果:

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

缓存刷新成功。

缓存初解(五)---SpringMVC基于注解的缓存配置--web应用实例的更多相关文章

  1. 缓存初解(三)---Spring3.0基于注解的缓存配置+Ehcache和OScache

    本文将构建一个普通工程来说明spring注解缓存的使用方式,关于如何在web应用中使用注解缓存,请参见: Spring基于注解的缓存配置--web应用实例 一.简介 在spring的modules包中 ...

  2. spring和ehcache整合,实现基于注解的缓存实现

    要实现基于注解的缓存实现,要求Spring的版本在3.1或以上版本. 首先需要在spring的配置文件中添加对缓存注解的实现: <?xml version="1.0" enc ...

  3. 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s

     1. Spring-mvc介绍 1.1市面上流行的框架 Struts2(比较多) Springmvc(比较多而且属于上升的趋势) Struts1(即将被淘汰) 其他 1.2  spring-mv ...

  4. springMVC基于注解的控制器

    springMVC基于注解的控制器 springMVC基于注解的控制器的优点有两个: 1.控制器可以处理多个动作,这就允许将相关操作写在一个类中. 2.控制器的请求映射不需要存储在配置文件中.使用re ...

  5. Struts2基于注解的Action配置

    使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了. 要使用注解方式,我们必须添加一个额外包:struts2-convention-plu ...

  6. 基于注解的bean配置

    基于注解的bean配置,主要是进行applicationContext.xml配置.DAO层类注解.Service层类注解. 1.在applicationContext.xml文件中配置信息如下 &l ...

  7. 8 -- 深入使用Spring -- 4...5 AOP代理:基于注解的“零配置”方式

    8.4.5 基于注解的“零配置”方式 AspectJ允许使用注解定义切面.切入点和增强处理,而Spring框架则可识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5 一样的 ...

  8. 基于注解的形式配置Bean

    基于注解的方式配置Bean:也就说我们在每个Bean的类名前面注解一下,Spring会自动帮我们扫描Bean放进IOC容器中 I基于注解的方式配置Bean(没有依赖关系的Bean)有两个步骤: 1组件 ...

  9. 10 Spring框架--基于注解的IOC配置

    1.工程环境搭建 2.基于注解的IOC配置 IOC注解的分类 (1)用于创建对象的 他们的作用就和在XML配置文件中编写一个<bean>标签实现的功能是一样的@Component: 作用: ...

随机推荐

  1. JS中的this用法详解

    随着对js的深入学习和使用,你会发现它里面包含了很多令人困惑的机制,比如对象.闭包.原型链继承等等,而这其中肯定包含令你现在或者曾经费解的this,如果你不把心一横,花点时间还真不明白这个this的用 ...

  2. jquery鼠标滑过提示title具体实现代码

    jquery鼠标滑过提示title的实现代码. 如下: <script type="text/javascript" src="http://ajax.google ...

  3. Delphi XE5教程9:基本语法元素

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误!也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者可 ...

  4. Excel中的宏--VBA的简单例子

    第一步:点击录制宏 第二步:填写宏的方法名 第三步:进行一系列的操作之后,关闭宏 第四步:根据自己的需要查看,修改宏 第六步:保存,一般是另存为,后缀名为.xlsm,否则宏语言不能保存. 到此为止恭喜 ...

  5. @RenderSection与@RenderBody

    _LayoutMain: <html> <head> @RenderSection("head") </head> <body> @ ...

  6. 社区发现算法问题&&NetworkX&&Gephi

    在做东西的时候用到了社区发现,因此了解了一下有关社区发现的一些问题 1,社区发现算法 (1)SCAN:一种基于密度的社团发现算法 Paper: <SCAN: A Structural Clust ...

  7. Lightmapping

    当游戏场景包含了大量的多边形时,实时光源和阴影对游戏性能的影响会很大.这时更适合使用Lightmapping技术,将光线效果预渲染成贴图使用到多边形上模拟光影效果.这种方式不用担心光源数量和阴影对性能 ...

  8. Android学习笔记:TabHost 和 FragmentTabHost(转)

    转自:http://www.cnblogs.com/asion/p/3339313.html   作者:Asion Tang   出处:http://asiontang.cnblogs.com   T ...

  9. 单例模式(.NET)

    问题描述:         单例模式 Singleton Pattern 问题解决: (1)单例模式简介: Singleton模式要求一个类有且仅有一个实例,并且提供了一个全局的访问点.这就提出了一个 ...

  10. 让你网页同时兼容FireFox和IE

    CSS 兼容要点:DOCTYPE 影响 CSS 处理 FireFox: div 设置 margin-left, margin-right 为 auto 时已经居中, IE 不行. FireFox: b ...