Introduction
by:http://opensource.atlassian.com/confluence/spring/display/DISC/AOP+Cache
I've written an AOP interceptor which allows you to specify which methods to cache for Spring beans.
Different cache providers are available: Memory HashMap, EHCache, OSCache (which is clusterable) and SwarmCache.
Example
We start by defining a bookManager object which has one method
which returns a List of Books that are related to the Book that is specified. This method is ideally suited for caching, since the related books will not change that often.<bean id="bookManager" class="com.example.BookManager"/>
Next we define the AOP interceptor which will cache the results. This example will return a cached result (the List of Books) instead of a call to bookManager.getRelated(Book), if the method is called with the same Book argument.
Available implementations of CacheInterceptor are:
- MemoryCacheInterceptor: a simple in-memory cache that's not meant for production
- EHCacheInterceptor: uses EHCache from Hibernate and should be configured in ehcache.xml as described in the EHCache documentation.
- SwarmCacheInterceptor: a clusterable cache implementation
- OSCacheInterceptor: uses OSCache from OpenSymphony and is the one used in this example
The cache in the example is expired after 15 minutes.
class ="org.springframework.aop.interceptor.cache.OSCacheInterceptor" >
< property name ="refreshPeriods" >
< props >
<!-- Cache the returned related books for 15 minutes -->
< prop key ="com.example.BookManager@getRelated" > 900 </ prop >
</ props >
</ property >
<!-- For caches not defined under 'refreshPeriods', use this value -->
< property name ="defaultRefreshPeriod" >
< value > -1 </ value >
</ property >
<!-- Which method to call for non-standard objects like String, Boolean or Number.
This method should be a simple method, like getId() or toString(), but should
uniquely identify the object. -->
< property name ="identifiers" >
< props >
<!-- If a method contains an argument com.example.Book,
the generated cache key contains the value of Book.getId() -->
< prop key ="com.example.Book" > getId </ prop >
</ props >
</ property >
</ bean >
refreshPeriods and defaultRefreshPeriod are properties that are specific for the OSCacheInterceptor. The best way to know how to configure a specific CacheInterceptor implementation is by having a look in the Javadoc (see the project.zip file).
refreshPeriods indicates how long the results will be cached in seconds. So a call to com.example.BookManager.getRelated() will be cached for 900 seconds (15 minutes).
When a method is intercepted that is not defined under refreshPeriods, the value of defaultRefreshPeriod will be used.
In order to be able to identify a call to the method with the same parameter, we use the identifiers property. Here you can list the function that needs to be called in order to get a unique identifier for this class. For each class that is used as an argument for the cached methods, specify the method name (which may not have any parameters). This is a better aproach than using the toString() method, since this method can produce long lines while most of the time a simple identifier is available. So in this example, book.getId() will be used to identify separate Book arguments. There is no need to specify arguments that are primitives (float, int), Strings or Numbers (Float, Integer, ...).
Now we wire the cacheInterceptor to the bookManager bean and we're done! Calls to bookManager.getRelated() will from now on be cached for 15 minutes.
Of course you can add as many beans to the cache as you want.
< bean id ="bookManagerAdvisor"
class ="org.springframework.aop.support.RegexpMethodPointcutAdvisor" >
< property name ="advice" >
< ref bean ="cacheInterceptor" />
</ property >
< property name ="patterns" >
< list >
< value > .*getRelated </ value >
</ list >
</ property >
</ bean >
< bean id ="bookManagerCacheProxyCreator"
class ="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
< property name ="beanNames" >
< value > bookManager </ value >
</ property >
< property name ="interceptorNames" >
< list >
< value > bookManagerAdvisor </ value >
</ list >
</ property >
</ bean >
相关推荐
Spring MVC、Mybatis、Spring 和 oscache 是Java Web开发中常用的四大框架,它们协同工作可以构建高效、可扩展的应用程序。下面将详细讲解这四个框架的配置文件以及它们在实际项目中的作用。 **Spring MVC** Spring...
jQuery+json+struts2+spring3(事务,AOP,IoC即DI)+hibernte3+EhCache+oscache+proxool+MySQL+SiteMesh+Gzip lj例子
综上所述,这个压缩包提供的配置文件涵盖了Spring MVC的Web应用开发、MyBatis的数据访问、Spring 3.0的依赖注入和AOP以及oscache的缓存管理等多个方面,对于理解和学习这些技术的集成应用非常有帮助。在实际项目中,...
首先,Spring框架是核心,它负责管理对象的生命周期,包括依赖注入(DI)和面向切面编程(AOP)。Spring通过`applicationContext.xml`配置文件来配置应用上下文,这里可以定义Bean、事务管理、连接池等。Spring还...
**Spring框架**是Java企业级应用开发的基石,它提供了全面的应用程序架构支持,包括依赖注入(DI)、面向切面编程(AOP)、事务管理、数据访问集成、MVC web框架等。Spring的核心在于其IoC(Inverse of Control)...
至于为什么选择 EhCache 而不是其他如 OSCache 或 JBossCache,尽管它们在性能上可能差异不大,但 Spring 对 EhCache 的官方支持使得集成更简便,减少了开发和维护的工作量。 综上所述,通过 Spring AOP 和 EhCache...
2. 配置Spring的ApplicationContext,定义Bean、AOP、事务管理等。 3. 配置SpringMVC的DispatcherServlet,包括视图解析器、拦截器等。 4. 配置Mybatis的SqlSessionFactory,结合Mybatis-Spring的...
在Web应用中,osCache常与Spring框架集成,利用Spring的AOP(面向切面编程)能力实现缓存的自动化管理。通过定义Bean并使用`@Cacheable`注解,可以轻松地为特定方法开启缓存功能。 总的来说,osCache提供了强大的...
在实际开发中,OSCache常与Spring框架结合使用,通过Spring的AOP(面向切面编程)来实现缓存注解,简化代码。例如,可以使用`@Cacheable`注解来标记一个方法的返回结果应被缓存: ```java @Cacheable(value = ...
1. **Web应用集成**:在Spring或Struts等Web框架中,可以通过AOP(面向切面编程)来拦截数据访问方法,自动进行缓存操作。 2. **数据分页**:对于分页查询,可以缓存每一页的结果,提高查询效率。 3. **分布式缓存...
例如,使用 Spring AOP,可以方便地在方法级别添加缓存控制。 总的来说,OSCache 提供了一个强大的缓存解决方案,通过合理的配置和使用,能够显著提升 Java 应用的性能。理解并熟练掌握 `oscache.properties` 的...
通过Spring AOP(面向切面编程)和Ehcache的结合使用,可以在Spring管理的应用中轻松实现数据缓存,提升应用性能。 首先,Ehcache是一个广泛使用的Java本地缓存框架,它能够缓存各种数据类型,如集合、对象等。使用...
- 结合Spring框架,可以通过AOP(面向切面编程)实现自动缓存管理,简化代码。 5. 注意事项: - 虽然缓存能提高性能,但过度依赖缓存可能导致数据不一致,需要合理设定缓存策略。 - 配置合适的缓存大小,避免...
aop.jar spring-dao.jar spring-hibernate.jar spring-jdbc.jar spring-mock.jar spring-orm.jar spring-remoting.jar spring-support.jar spring-webmvc.jar
-- Spring AOP config配置切点 --> <aop:config> <aop:pointcut expression="execution(* com.org.service.*.*(..))" id="bussinessService" /> <aop:advisor advice-ref="txAdvice" pointcut-ref=...
proxool-0.8.3.jar spring-agent.jar spring-aop-2.0.xsd spring-aop.jar spring-beans-2.0.xsd spring-beans.jar spring-context.jar spring-core.jar spring-dao.jar spring-hibernate3.jar spring-ibatis.jar ...
Spring的IoC容器负责管理对象的生命周期和依赖关系,而Spring的AOP支持则可以方便地进行日志记录、性能监控、事务管理等横切关注点的处理。在与Hibernate的整合中,Spring可以帮助简化数据库事务的管理,提供声明式...