- 浏览: 329020 次
- 性别:
- 来自: 天津
文章分类
最新评论
-
xing0029:
这样配置的话 事物不成功啊 还有什么地方需要注意的么 可以加我 ...
JTA集成JOTM或Atomikos配置分布式事务(Tomcat应用服务器) -
mengqingyu:
liuxiaolu 写道jotm的我的没有成功,楼主能否帮助一 ...
JTA集成JOTM或Atomikos配置分布式事务(Tomcat应用服务器) -
liuxiaolu:
jotm的我的没有成功,楼主能否帮助一下
JTA集成JOTM或Atomikos配置分布式事务(Tomcat应用服务器) -
aptech406328627:
求解救,没弄好QQ:1053942353
Spring邮件发送(可带附件,模板,群发,异步发送等功能) -
ghpaas:
web可视化自定义表单推荐使用GForms开发平台(http: ...
在线表单设计器设计原理
AOP+Ehcache 实现缓存功能
设计思路:查询数据,通过AOP拦截需要缓存的方法,在方法进入之前进入拦截器,通过包、类、方法名称作为key获取当前缓存对象结果,如果为空,则执行真正的方法,如果有缓存对象,则直接返回。删除和修改数据,每次都在此之前调用拦截器方法清除相关缓存对象。
1.需要引入AOP和Ehcache相关Jar文件
2.编写切面类里面写上拦截时的业务逻辑
3.配置文件中配置AOP和类的实例化
4.引入ehcache.xml文件
设计思路:查询数据,通过AOP拦截需要缓存的方法,在方法进入之前进入拦截器,通过包、类、方法名称作为key获取当前缓存对象结果,如果为空,则执行真正的方法,如果有缓存对象,则直接返回。删除和修改数据,每次都在此之前调用拦截器方法清除相关缓存对象。
1.需要引入AOP和Ehcache相关Jar文件
2.编写切面类里面写上拦截时的业务逻辑
import java.io.Serializable; import java.util.List; import net.sf.ehcache.Cache; import net.sf.ehcache.Element; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.util.Assert; /** * * 类功能描述:AOP+Ehcache 拦截需要缓存的方法,插入点方法执行前后 * * @author <a href="mailto:qingyu.meng21@gmail.com">mengqingyu </a> * @version $Id: codetemplates.xml,v 1.1 2009/03/06 01:13:01 mengqingyu Exp $ * Create: 2011-12-12 下午04:27:44 */ public class MethodCacheInterceptor implements MethodInterceptor, InitializingBean { private static final Log logger = LogFactory.getLog(MethodCacheInterceptor.class); private Cache cache; public void setCache(Cache cache) { this.cache = cache; } public MethodCacheInterceptor() { super(); } /** * 拦截Service/DAO的方法,并查找该结果是否存在,如果存在就返回cache中的值, * 否则,返回数据库查询结果,并将查询结果放入cache */ @Override public Object invoke(MethodInvocation invocation) throws Throwable { Object result; String cacheKey = getCacheKey(invocation.getThis().getClass().getName() , invocation.getMethod().getName(), invocation.getArguments()); Element element = cache.get(cacheKey); logger.warn("检查缓存:" + cacheKey); if (element == null) { logger.warn("创建缓存......"); result = invocation.proceed(); element = new Element(cacheKey, (Serializable) result); cache.put(element); } List list = cache.getKeys(); return element.getValue(); } /** * * @function:获得cache key的方法,cache key是Cache中一个Element的唯一标识 * cache key包括 包名+类名+方法名,如com.co.cache.service.UserServiceImpl.getAllUser * @param targetName * @param methodName * @param arguments * @return * @author: mengqingyu 2011-12-12 下午04:35:21 */ private String getCacheKey(String targetName, String methodName, Object[] arguments) { StringBuilder sb = new StringBuilder(); sb.append(targetName).append(".").append(methodName); if (arguments != null) { for(Object o:arguments){ sb.append(".").append(o); } } return sb.toString(); } /** * implement InitializingBean,检查cache是否为空 */ public void afterPropertiesSet() throws Exception { Assert.notNull(cache, "缓存未初始化"); } } package com.berheley.bi.aop; import java.lang.reflect.Method; import java.util.List; import net.sf.ehcache.Cache; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.aop.AfterReturningAdvice; import org.springframework.beans.factory.InitializingBean; import org.springframework.util.Assert; /** * * 类功能描述:AOP+Ehcache 拦截需要缓存的方法,插入点方法执行之后 * * @author <a href="mailto:qingyu.meng21@gmail.com">mengqingyu </a> * @version $Id: codetemplates.xml,v 1.1 2009/03/06 01:13:01 mengqingyu Exp $ * Create: 2011-12-12 下午04:27:44 */ public class MethodCacheAfterAdvice implements AfterReturningAdvice, InitializingBean { private static final Log logger = LogFactory.getLog(MethodCacheAfterAdvice.class); private Cache cache; public void setCache(Cache cache) { this.cache = cache; } public MethodCacheAfterAdvice() { super(); } /** * 拦截Service/DAO的方法,并查找该结果是否存在,如果存在则删除结果, * 否则忽略,此方法为了保证更新数据库同时同步缓存 */ @Override public void afterReturning(Object arg, Method method, Object[] arguments, Object obj) throws Throwable { String className = obj.getClass().getName(); List<String> list = cache.getKeys(); for(String cacheKey:list){ if(cacheKey.startsWith(className)){ cache.remove(cacheKey); logger.debug("删除缓存:" + cacheKey); } } } /** * implement InitializingBean,检查cache是否为空 */ public void afterPropertiesSet() throws Exception { Assert.notNull(cache, "缓存未初始化"); } }
3.配置文件中配置AOP和类的实例化
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <!-- AOP+Ehcache by mengqingyu--> <!-- 引用ehCache的配置 --> <bean id="defaultCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation"> <value>classpath:ehcache.xml</value> </property> </bean> <!-- 定义ehCache的工厂,并设置所使用的Cache name 如找不到则使用默认缓存配置defaultCache--> <bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean"> <property name="cacheManager"> <ref local="defaultCacheManager"/> </property> <property name="cacheName"> <value>systemCache</value> </property> </bean> <!-- find/create cache拦截器 --> <bean id="methodCacheInterceptor" class="com.berheley.bi.aop.MethodCacheInterceptor"> <property name="cache"> <ref local="ehCache" /> </property> </bean> <!-- find/create cache拦截器 --> <bean id="methodCacheAfterAdvice" class="com.berheley.bi.aop.MethodCacheAfterAdvice"> <property name="cache"> <ref local="ehCache" /> </property> </bean> <!-- 自动代理 --> <aop:config> <!-- 这里可添加多个切入点 --> <aop:advisor pointcut="execution(* com.berheley.bi.report.service.impl.SummaryReportServiceImpl.getSource*(..))" advice-ref="methodCacheInterceptor" /> <aop:advisor pointcut="execution(* com.berheley.bi.report.service.impl.SummaryReportServiceImpl.getPrefixAndSuffix*(..))" advice-ref="methodCacheAfterAdvice" /> </aop:config> </beans>
4.引入ehcache.xml文件
<!-- AOP+Ehcache by mengqingyu--> <!-- diskStore 当内存缓存数量超过设置数量时,将数据写到硬盘中,path为硬盘相对路径 maxElementsInMemory 缓存中最大允许创建的对象数 eternal 缓存中对象是否为永久,如果true过期设置将无效 timeToIdleSeconds 自最后读取开始有效时间 timeToLiveSeconds 自创建开始有效时间 overflowToDisk 内存不足时是否启用磁盘缓存 --> <ehcache> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false" diskExpiryThreadIntervalSeconds="120"/> <cache name="systemCache" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="259200000" timeToLiveSeconds="259200000" overflowToDisk="true"/> </ehcache>
发表评论
-
Spring扩展点
2015-08-17 11:14 2582Spring扩展点 1.IOC生成类全名 <!-- ... -
Spring AOP动态代理
2015-01-25 22:23 756package com.test.dynamicproxy; ... -
Spring加载资源并装配对象的过程
2015-01-25 22:20 10301. 定义好Spring的配置文件。 2. 通过Resourc ... -
(转载)Spring Bean 初始化过程
2013-05-10 13:10 1128Spring 的几个接口 1.InitializingBea ... -
(转载)浅谈Spring事务隔离级别
2013-04-15 14:45 956一、Propagation : key属 ... -
AOP+LOG4J日志框架(自定义注解)
2013-04-11 15:00 2703工作中用到日志功能,参考网上一些资料,写了个比较通用的日志框架 ... -
(转载)spring AOP获得session
2013-02-20 17:01 2370由于Spring 的AOP面向切面编程,与Servlet容器没 ... -
Quartz(三)原理及源码分析
2012-10-30 14:56 14015quartz配置文件中可以通过以下两种配置读取方式 org.q ... -
spring-security3(二)源码分析
2012-04-06 17:08 8160利用断点走了一遍spring-security源码的核心部分, ... -
spring-security3(一)配置详解及API扩展(包含ajax返回)
2012-04-06 17:05 3782最近对spring-security3做了一些初步了解,搜集了 ... -
JTA集成JOTM或Atomikos配置分布式事务(Tomcat应用服务器)
2010-01-15 21:01 6634一.以下介绍Spring中直接集成JOTM提供JTA事务管理、 ... -
Quartz(二)整合Spring容器中bean及动态调度任务
2010-01-13 10:01 7688Quartz 是开源任务调度框架中的翘首,它提供了强大任务 ... -
Quartz(一)在Spring中设置动态定时任务
2009-05-17 07:09 7759什么是动态定时任务: 是由客户制定生成的,服务端只知道该去执行 ... -
Spring邮件发送(可带附件,模板,群发,异步发送等功能)
2009-05-17 06:27 8685以下是我对spring发送邮件的总结: 分别使用了两种方法:单 ...
相关推荐
【EhCache】Java缓存框架使用EhCache结合Spring AOP EhCache是一个纯Java的进程内缓存框架,具有如下特点: 1. 快速简单,非常容易和应用集成。 2.支持多种缓存策略 。 3. 缓存数据有两级:内存和磁盘,因此无需...
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。Ehcache是一种广泛使用的开 源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,...
常用功能:会话(session),安全性(rbac权限框架),shiro(rbac权限框架),oauth(第三方登录),logback(日志),log-aop(AOP方式日志),ehcache(ehcache缓存),异常(统一异常处理),Async(异步
plus(快速操作Mybatis)、BeetlSQL(强大的ORM框架)、upload(本地文件上传和七牛云文件上传)、redis(缓存)、ehcache(缓存)、email(发送各种类型邮件)、task(基础定时任务)、quartz(动态管理定时任务)、xxl-job(分布式...
2、Spring Cache利用了AOP,实现了基于注解的缓存功能,并且进行了合理的抽象,业务代码不用关心底层是使用了什么缓存框架,只需要简单地加一个注解,就能实现缓存功能了,做到了对代码侵入性做小。 3、由于市面上的...
【AOP核心】 com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar spring-aspects-4.0.0.RELEASE.jar 【IOC核心】...
Context-support模块:提供了对第三方库嵌入Spring应用的集成支持,比如缓存(EhCache、Guava、JCache)、邮件服务(JavaMail)、任务调度(CommonJ、Quartz)和模板引擎(FreeMarker、JasperReports、速率)。 SpEL模块:...
plus(`快速操作Mybatis`)、BeetlSQL(`强大的ORM框架`)、upload(`本地文件上传和七牛云文件上传`)、redis(`缓存`)、ehcache(`缓存`)、email(`发送各种类型邮件`)、task(`基础定时任务`)、quartz(`动态管理定时任务`)...
该项目已成功集成 actuator(监控)、admin(可视化监控)、logback(日志)、aopLog(通过AOP...plus(快速操作Mybatis)、BeetlSQL(强大的ORM框架)、upload(本地文件上传和七牛云文件上传)、redis(缓存)、ehcache(缓存)、ema…
plus(快速操作Mybatis)、BeetlSQL(强大的ORM框架)、upload(本地文件上传和七牛云文件上传)、redis(缓存)、ehcache(缓存)、email(发送各种类型邮件)、task(基础定时任务)、quartz(动态管理定时任务)、xxl-job(分布式...
阿里云java短信验证码源码 springboot-learning-3 springboot框架相关技术的案例项目及使用springboot框架时的需...SpringBoot整合Ehcache高速缓存 SpringBoot-Chart ----- Spring Boot整合JFreeChart各种图表绘制类
spring boot demo是一个用于深度学习并实战...强大的ORM框架)、upload( 本地文件上传和七牛云文件上传)、redis( 缓存)、ehcache( 缓存)、email( 发送各种类型邮件)、task( 基础定时任务)、quartz( 动态管理定时任务)等
plus(快速操作Mybatis)、BeetlSQL(强大的ORM框架)、upload(本地文件上传和七牛云文件上传)、redis(缓存)、ehcache(缓存)、email(发送各种类型邮件)、task(基础定时任务)、quartz(动态管理定时任务)、xxl-job(分布式...
BeetlSQL(强大的ORM框架),upload(本地文件上传和七牛云文件上传),redis(缓存),ehcache(缓存),email(发送各种类型邮件),任务(基础定时任务),Quartz(动态管理定时任务),xxl-job(分布式定时任务...
灵活的缓存服务,支持EhCache、Redis和多级缓存(MultiLevel)技术;配置简单的MVC架构,强大且易于维护和扩展,支持RESTful风格,支持JSP、HTML、Binary、Freemarker、Velocity等多种视图技术。
│ │ ├─ehcache.xml----------------ehcache缓存配置 │ │ │ └─webapp----------------web页面和静态资源存放的目录 │ 注:SpringBoot项目默认不支持将静态资源和模板(web页面)放到webapp目录,但是个人感觉...
(10) spring-support.jar 这个jar文件包含支持缓存Cache(ehcache)、JCA、JMX、邮件服务(Java Mail、COS Mail)、任务计划Scheduling(Timer、Quartz)方面的类。 (11) spring-web.jar 这个jar文件包含Web应用...
提供了AOP、RPC、分布式缓存、限流、降级、熔断、统一配置中心、swagger api自动生成、Opentracing数据追踪、metrics数据监控、分布式session、代码生成器、shiro安全控制等功能。 Jboot 的核心组件 1、MVC (基于...
5. 利用ehcache框架对经常调用的查询进行缓存,提升运行速度,具体请见ConstantFactory类中@Cacheable标记的方法。 6. controller层采用map + warpper方式的返回结果,返回给前端更为灵活的数据,具体参见...
这个jar文件包含支持缓存Cache(ehcache)、JCA、JMX、邮件服务(Java Mail、COS Mail)、任务计划Scheduling(Timer、Quartz)方面的类。 UI方面的用来与模板(Templating)引擎如 Velocity、FreeMarker、...