- 浏览: 1526655 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (225)
- JAVA (27)
- Spring (49)
- Linux (51)
- JavaScript (8)
- Scrum (23)
- IDE (2)
- JSON (2)
- Solr (0)
- Webharvest (0)
- Hibernate (8)
- 杂谈 (3)
- Windows 7 (4)
- 持续集成 (23)
- tomcat (3)
- Android (1)
- SpringSecurity (11)
- Maven (9)
- jotm (3)
- C3P0 (1)
- Active Directory (2)
- cas (1)
- JQuery (2)
- ajax (1)
- plsql (2)
- nginx (4)
- apache (1)
- thrift (7)
- python (3)
- oracle (4)
- php (2)
- redis (1)
- fedora (1)
- windows7 (0)
- SVN (1)
- NFS (1)
- SAMBA (1)
- Atomikos (1)
- apache-poi (1)
- mysql (2)
- vncserver (1)
- mac (2)
- firefox (1)
- JIRA (1)
- p6spy (1)
- git (1)
- github (1)
- gitlab (1)
- gogs (1)
- Druid (1)
- MyBatis (1)
- docker (8)
- zabbix (1)
最新评论
-
lialatd:
您好,我用您的方法通过java api往jira系统中添加is ...
JIRA REST API ---- JAVA -
sprcen945:
可以了,是因为没加intercept-url 的拦截, 尼玛, ...
SpringSecurity3.X--Cas client 配置 -
sprcen945:
请问为什么我配了security.xml后切入点不起作用(之前 ...
SpringSecurity3.X--Cas client 配置 -
linxingyul:
根据楼主的代码 继承了WebMvcConfigurationS ...
SpringMVC4零配置--Web上下文配置【MvcConfig】 -
java_老头:
MvcConfig.java的FilterType.ANNOT ...
SpringMVC4零配置--Web上下文配置【MvcConfig】
之前为大家介绍了如何使用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
缓存刷新成功。
评论
访问http://localhost:8080/cache/demo.do
请求两次的输出结果:
Processing testCaching
NameId:0
NameId:0
接着我们刷新该缓存,访问http://localhost:8080/cache/demoFulsh.do
输出结果:
Processing testFlushing
再请求http://localhost:8080/cache/demo.do
输出结果:
Processing testCaching
NameId:0
404是因为你项目名字都没有改,把访问的路径中的cache改成你的项目名称
发表评论
-
Druid学习笔记
2016-10-07 11:55 2448官方网站:https://github.com/aliba ... -
Spring Cache注解+Redis
2015-01-15 13:36 54438Spring3.1 Cache注解 依赖jar包: ... -
Spring Cache注解+Memcached
2015-01-12 16:11 20395Spring3.1 Cache注解 依赖jar包: ... -
Spring4+Hibernate4+Atomikos3.3多数据源事务管理
2014-09-25 10:46 8337Spring3+后不再对JTOM提供支持,所以可以改用At ... -
SpringMVC4零配置--Web上下文配置【MvcConfig】
2014-09-10 18:22 73357与SpringSecurity的配置类似,spring同样 ... -
SpringMVC4零配置--SpringSecurity相关配置【SpringSecurityConfig】
2014-09-10 18:22 71921SpringSecurity的配置相对来说有些复杂,如果 ... -
SpringMVC4零配置--应用上下文配置【AppConfig】
2014-09-10 18:21 26477从spring3.0开始,Spring将JavaConfi ... -
SpringMVC4零配置--web.xml
2014-09-10 18:21 98638servlet3.0+规范后,允许servlet,filt ... -
SpringMVC4零配置
2014-09-05 19:11 89909基于Servlet3.0规范和SpringMVC4注解式配 ... -
SpringSecurity3.X--LDAP:AD配置
2014-07-08 17:08 5521前面介绍过基于本地数据库验证的方式,参考http://ha ... -
Thrift--JSClient
2013-09-26 14:45 5920thrift提供了基于jquery--ajax的客户端调用 ... -
Thrift--Spring集成ThriftServlet
2013-09-25 11:42 11082Thrift除了可以通过TCP协议访问,还可以通过HTTP ... -
Thrift转SpringHttpInvoker
2013-09-24 13:26 1732关于在spring中集成Thrift请参看:http://h ... -
Spring集成Thrift--Server AND Client
2013-09-04 20:13 13716Thrift网上有N多教程, ... -
C3P0配置实战
2012-09-04 18:34 51852C3P0: 一个开源的JDBC连接池,它实现了数据源和JN ... -
spring+jotm 多数据源事务管理(三)JNDI+Tomcat
2012-06-07 16:27 5224spring+jotm 多数据源事务管理系列 spr ... -
spring+jotm 多数据源事务管理(二)hibernate
2012-06-07 11:20 2816spring+jotm 多数据源事务管理系列 spr ... -
spring+jotm 多数据源事务管理(一)jdbc
2012-06-07 11:00 5235spring+jotm 多数据源事务管理系列 spr ... -
SpringSecurity3.X--Cas client 配置之配置session-management遇到的问题(2)
2011-10-27 14:19 2101关于“SpringSecurity3.X--Cas clien ... -
SpringSecurity3.X--Cas client 配置之配置session-management遇到的问题
2011-10-26 18:56 7867关于“SpringSecurity3.X--Cas ...
相关推荐
博客http://blog.csdn.net/poorcoder_/article/details/59541710的代码。主要描述spring通过注解整合redis用作缓存的实例。
本实例完美的实现了Spring基于注解整合Redis,只需在相应方法上加上注解便可实现操作redis缓存的功能,具体实现方法与运行测试方法请参见博文:http://blog.csdn.net/l1028386804/article/details/52141372
ehcache全解析 利用Spring和EHCache缓存结果 Hibernate+ehcache二级缓存技术 Spring基于注解的缓存配置--web应用实例 http://zjava.org.ru/
JetCache是一个基于java的缓存系统封装,提供统一的API和注解简化缓存的使用。JetCache提供了比SpringCache更强大的注解,可以原生的支持TTL、两级缓存、分布式自动刷新,提供了Cache接口用于手工缓存操作。当前有四...
本项目是基于spring4+springmvc+mybatis+redis缓存 注解方式以及spring-session共享session搭建的完整实例。
4.11.2 使用基于Java类的配置信息启动Spring容器 4.12 不同配置方式比较 4.13 小结 第5章 Spring容器高级主题 5.1 Spring容器技术内幕 5.1.1 内部工作机制 5.1.2 BeanDefinition 5.1.3 InstantiationStrategy 5.1.4 ...
3.8.4. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.9.1. 使用Singleton-helper类 4. 资源 4.1. 简介 4.2. Resource 接口 4.3. 内置 Resource 实现 4.3.1. UrlResource 4.3.2. Class...
Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序。它提供了很多方面的功能,比如依赖注入、面向方面编程(AOP)、数据访问抽象及ASP.NET扩展等等。Spring.NET以Java版的Spring框架为...
使用Spring注解方式管理事务与传播行为详解 24.使用Spring配置文件实现事务管理 25.搭建和配置Spring与Hibernate整合的环境 26.Spring集成的Hibernate编码与测试 27.Struts与Spring集成方案1(Struts集成Spring) ...
4.11.2 使用基于Java类的配置信息启动Spring容器 4.12 不同配置方式比较 4.13 小结 第5章 Spring容器高级主题 5.1 Spring容器技术内幕 5.1.1 内部工作机制 5.1.2 BeanDefinition 5.1.3 InstantiationStrategy 5.1.4 ...
主要介绍了spring boot注解方式使用redis缓存操作,结合实例形式分析了spring boot注解方式使用redis缓存相关的依赖库引入、注解使用及redis缓存相关操作技巧,需要的朋友可以参考下
Spring3.2 Hibernate4.2 JPA2全注解实例.采用JTA事务管理,配置ehcache为二级缓存,在glassfish3.2.2和postgresql9测试通过。参考网上的资料整理。
DWR3.0 Spring3.2 Hibernate4.2 JPA全注解实例。采用JTA事务管理,配置ehcache为二级缓存,在glassfish3.2.2和postgresql9测试通过。参考网上的资料整理。.
Wicket6.7 Spring3.2 Hibernate4.2 EJB全注解实例.采用JTA事务管理,配置ehcache为二级缓存,在glassfish3.2.2和postgresql9测试通过。参考网上的资料整理。
而Spring Boot 是Spring 主推的基于“习惯优于配置”的原则,让你能够快速搭建应用的框架,从而使得Java EE 开发变得异常简单。 《JavaEE开发的颠覆者: Spring Boot实战》从Spring 基础、Spring MVC 基础讲起,从而...
整合Apollo应用配置中心 Aop 整合阿里云Acm应用配置中心 基于切面与redis实现统一方法缓存 整合Dubbo 整合elasticsearch 拦截器 基于boot+Quartz定时任务调度平台 kafka 基于Redis分布式限流组件设计与使用实例 发送...
而Spring Boot 是Spring 主推的基于“习惯优于配置”的原则,让你能够快速搭建应用的框架,从而使得Java EE 开发变得异常简单。 《JavaEE开发的颠覆者: Spring Boot实战》从Spring 基础、Spring MVC 基础讲起,从而...