首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Annotation兑现缓存,Bean指定代码缓存类

2012-09-03 
Annotation实现缓存,Bean指定代码缓存类package org.frame.base.annotation.supportimport java.lang.ref

Annotation实现缓存,Bean指定代码缓存类



package org.frame.base.annotation.support;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Proxy;import java.util.Set;import org.frame.base.annotation.intercept.ProxyHandler;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;/** * 使用Ehcache代理来实现缓存拦截  * */public class EhCacheBeanPostProcessor implements BeanPostProcessor{private Set<String> beans;public void setBeans(Set<String>  beans) {this.beans = beans;}@Overridepublic Object postProcessAfterInitialization(Object target, String beanName)throws BeansException { return target;}/** * will be process the beans define in properties * if you don't want to define * you can define the pach model  *DAO * you can define the pach model  *Service * you can define the pach model  *Action * but i sugguest you define */@Overridepublic Object postProcessBeforeInitialization(Object target, String beanName)throws BeansException {if(beans.contains(beanName)){Class<?>[] interfaceClass = target.getClass().getInterfaces();InvocationHandler handler = new ProxyHandler(target);Object proxyObj = Proxy.newProxyInstance(target.getClass().getClassLoader(),interfaceClass, handler);return proxyObj;}else{return target;}} }

指定代理的类,这样去除缓存很简单,代码不需要更改,只需要从拦截的beans设置里面去掉该beanName,缓存就没了,添加只需要添加该bean定义。

热点排行