Spring aop 使用aspectjrt配置切面+ehcache简单配置



1.准备好ehcache和spirngjar包

2.配置文件<?xml version="1.0" encoding="UTF-8"?>

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
	">
	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
	<!-- - - - - - - - - - - - - - - - - Supernatural Separator - - - - - - - - - - - - - - - - -->
	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  
	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
	<!-- - - - - - - - - - - - - - - - - Supernatural Separator - - - - - - - - - - - - - - - - -->
	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
	<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
		<property name="configLocation">    
			<value>classpath:ehcache.xml</value>    
		</property>   
	</bean>  
      
	<bean id="methodCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">  
		<property name="cacheManager">  
			<ref local="cacheManager"/>  
		</property>  
		<property name="cacheName">  
			<value>com.rmn190.MethodCache</value>  
		</property>  
	</bean>
	
	  
	<bean id="wcmDdtPublishServiceAspect" class="interceptor.WcmDdtPublishServiceAspect">
	<property name="cache">  
			<ref local="methodCache"/>  
	</property>  
	</bean>  
	<aop:config>  
		<aop:pointcut id="getOnePointcut" expression="execution(* cn.com.capinfo.rd.cap_cloud.wcm.wcm_ddt_manager.daos_and_services.services.impls.WcmDdtPublishServiceImpl.getDdsInstanceById(..))"/>  
		<aop:aspect ref="wcmDdtPublishServiceAspect">  
			<!-- <aop:before pointcut-ref="pointcut" method="before"/>
			<aop:after-returning pointcut-ref="pointcut" method="afterReturn" />
			<aop:after pointcut-ref="pointcut" method="after" /> -->
			<aop:around pointcut-ref="getOnePointcut" method="doAroundGetOne"/>
		</aop:aspect>  
	</aop:config>
	<aop:config>  
		<aop:pointcut id="getListPointcut" expression="execution(* cn.com.capinfo.rd.cap_cloud.wcm.wcm_ddt_manager.daos_and_services.services.impls.WcmDdtPublishServiceImpl.getDdsInstanceByIds(..))"/>  
		<aop:aspect ref="wcmDdtPublishServiceAspect">  
			<!-- <aop:before pointcut-ref="pointcut" method="before"/>
			<aop:after-returning pointcut-ref="pointcut" method="afterReturn" />
			<aop:after pointcut-ref="pointcut" method="after" /> -->
			<aop:around pointcut-ref="getListPointcut" method="doAroundGetList"/>
		</aop:aspect>  
	</aop:config>
	
	<bean id="wcmDdtManagerServiceAspect" class="interceptor.WcmDdtManagerServiceAspect">
		<property name="cache">  
			<ref local="methodCache"/>  
	</property>  
	</bean>
	
		<aop:config>  
		<aop:pointcut id="updateCacheAspect" expression="execution(* cn.com.capinfo.rd.cap_cloud.wcm.wcm_ddt_manager.daos_and_services.services.impls.WcmDdtManagerServiceImpl.saveDdsDatasAndIndexToES(..))"/>  
		<aop:pointcut id="deleteCacheAspect" expression="execution(* cn.com.capinfo.rd.cap_cloud.wcm.wcm_ddt_manager.daos_and_services.services.impls.WcmDdtManagerServiceImpl.destroyDdsInstanceById(..))"/>  
		<aop:aspect ref="wcmDdtManagerServiceAspect">  
			<!-- <aop:before pointcut-ref="pointcut" method="before"/>
			<aop:after-returning pointcut-ref="pointcut" method="afterReturn" />
			<aop:after pointcut-ref="pointcut" method="after" /> -->
			<aop:after-returning pointcut-ref="updateCacheAspect" method="doAfterSaveOrUpdate" returning="result"/>
			<aop:around pointcut-ref="deleteCacheAspect" method="doAroundDelete"/>
		</aop:aspect>  
	</aop:config>
</beans>
<ehcache>  
	<!-- Sets the path to the directory where cache .data files are created.  

		 If the path is a Java System Property it is replaced by  
		 its value in the running VM.  

		 The following properties are translated:  
		 user.home - User's home directory  
		 user.dir - User's current working directory  
		 java.io.tmpdir - Default temp file path -->  
	<diskStore path="java.io.tmpdir"/>  
	<!--Default Cache configuration. These will applied to caches programmatically created through  
		the CacheManager.  

		The following attributes are required:  

		maxElementsInMemory            - Sets the maximum number of objects that will be created in memory  
		eternal(永恒)                        - Sets whether elements are eternal. If eternal,  timeouts are ignored and the  
										 element is never expired(过期).  
		overflowToDisk                 - Sets whether elements can overflow to disk when the in-memory cache  
										 has reached the maxInMemory limit.  

		The following attributes are optional:  
		timeToIdleSeconds              - Sets the time to idle for an element before it expires.  
										 i.e. The maximum amount of time between accesses before an element expires  
										 Is only used if the element is not eternal.  
										 Optional attribute. A value of 0 means that an Element can idle for infinity.  
										 The default value is 0.  
		timeToLiveSeconds              - Sets the time to live for an element before it expires.  
										 i.e. The maximum time between creation time and when an element expires.  
										 Is only used if the element is not eternal.  
										 Optional attribute. A value of 0 means that and Element can live for infinity.  
										 The default value is 0.  
		diskPersistent                 - Whether the disk store persists between restarts of the Virtual Machine.  
										 The default value is false.  
		diskExpiryThreadIntervalSeconds- The number of seconds between runs of the disk expiry thread. The default value  
										 is 120 seconds.  
		-->  
	<!-- maxElementsInMemory设定内存中创建对象的最大值 -->  
	<!-- eternal设置元素(译注:内存中对象)是否永久驻留。如果是,将忽略超 时限制且元素永不消亡。-->  
	<!-- overflowToDisk设置当内存中缓存达到 maxInMemory 限制时元素是否可写到磁盘上 -->  
	<!-- timeToIdleSeconds设置某个元素消亡前的停顿时间。  也就是在一个元素消亡之前,两次访问时间的最大时间间隔值。  
					 这只能在元素不是永久驻留时有效(译注:如果对象永恒不灭,则 设置该属性也无用)。  
			   如果该值是 0 就意味着元素可以停顿无穷长的时间。                                   
		-->  
	<!-- timeToLiveSeconds为元素设置消亡前的生存时间。 也就是一个元素从构建到消亡的最大时间间隔值。 这只能在元素不是永久驻留时有效。 
		-->  
	<!-- diskPersistent是否disk store在虚拟机启动时持久化。默认为false -->  
	<!-- diskExpiryThreadIntervalSeconds运行disk终结线程的时间,默认为120秒 -->  
	<defaultCache  
		maxElementsInMemory="10000"   
		eternal="false"   
		overflowToDisk="true"   
		timeToIdleSeconds="500"   
		timeToLiveSeconds="1000"   
		diskPersistent="false"   
		diskExpiryThreadIntervalSeconds="120"/>   
	<cache name="com.rmn190.MethodCache"  
		maxElementsInMemory="10"  
		eternal="false"  
		timeToIdleSeconds="200"  
		timeToLiveSeconds="300"  
		overflowToDisk="true"  
		/>      
</ehcache>  


package interceptor;

import java.io.Serializable;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.capinfo.rd.cap_cloud.wcm.wcm_ddt_manager.daos_and_services.services.WcmDdtPublishService;

public class WcmDdtManagerServiceAspect {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    private Cache cache;

    public void setCache(Cache cache) {
        this.cache = cache;
    }

    @Autowired
    protected WcmDdtPublishService wcmDdtPublishService;

    public void setWcmDdtPublishService(WcmDdtPublishService wcmDdtPublishService) {
        this.wcmDdtPublishService = wcmDdtPublishService;
    }
    
    public Object doAfterSaveOrUpdate(Object result) throws Throwable {
        System.out.println();
        Object returnValue;
        returnValue = result;
        System.out.println("数据的  id   = "+returnValue.toString());
        Object ob = wcmDdtPublishService.getDdsInstanceById(returnValue.toString());
        String cacheKey = getCacheKey(returnValue.toString());
        cache.remove(cacheKey);
        // cache method result
        // 下面方法执行后,将cacheKey与数据集连起来,cacheKey是用来标识这个element的标志,我们可以有多个element(各自是来自不同的数据访问方法而形成的),区分它们就是用cacheKey,
        Element element = new Element(cacheKey, (Serializable) ob);// 这里的新生成后的element,含有cacheKey,还在element创建时间,访问时间,还有命令次数等cache的属性,我觉得它就像是一个小cache一样,下次要不要更新它就要看它的这些属性来决定。
        cache.put(element);// 放入cache中
        return true;
    }
    
    public Object doAroundDelete(ProceedingJoinPoint jp) throws Throwable {
        Object[] arguments = jp.getArgs();
        String id = arguments[0].toString();
        jp.proceed(arguments);
        String cacheKey = getCacheKey(id);
        cache.remove(cacheKey);
        return arguments;
    }
    private String getCacheKey(Object arguments) {

        StringBuffer sb = new StringBuffer();
        sb.append("wcmDdtPublishService").append(".").append("getDdsInstanceById");
        if ((arguments != null)) {
            sb.append(".").append(arguments);
        }

        return sb.toString();
    }
}
package interceptor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
public class WcmDdtPublishServiceAspect {
    
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    private Cache cache;
    public void setCache(Cache cache) {
        this.cache = cache;
    }
    public Object doAroundGetOne(ProceedingJoinPoint jp) throws Throwable{

        Object[] arguments = jp.getArgs();
        Object result;
        String cacheKey = getCacheKey("wcmDdtPublishService", "getDdsInstanceById", arguments);// 这里得出的是:manager.PersonManagerImpl.getList
        System.out.println(cacheKey+" @@@@@@@@sdasda");
        Element element = cache.get(cacheKey);
        if(null == element){
            System.out.println("set into cache");
            result= jp.proceed(arguments);
            element = new Element(cacheKey, (Serializable) result);// 这里的新生成后的element,含有cacheKey,还在element创建时间,访问时间,还有命令次数等cache的属性,我觉得它就像是一个小cache一样,下次要不要更新它就要看它的这些属性来决定。
            cache.put(element);
        }
        System.out.println("out cache");
        return element.getObjectValue();
    }

     private String getCacheKey(String targetName, String methodName, Object[] arguments) {
        
        StringBuffer sb = new StringBuffer();
        sb.append(targetName).append(".").append(methodName);
        if ((arguments != null) && (arguments.length != 0)) {
            for (int i = 0; i < arguments.length; i++) {
                sb.append(".").append(arguments[i]);
            }
        }
        return sb.toString();
    }
     
     
     public Object doAroundGetList(ProceedingJoinPoint jp) throws Throwable{

         String targetName = jp.getThis().getClass().getName();// 这个表示哪个类调用(或触发)了这个MethodCacheInterceptor,如里的:manager.PersonMagagerImpl
         Object[] arguments = jp.getArgs();
         Object result;
         List< Object> ddsInstances = new ArrayList<Object>();
         List<String> cacheKeys = getCacheKeys(targetName, "getDdsInstanceByIds", arguments);// 这里得出的是:manager.PersonManagerImpl.getList
         for(String cacheKey : cacheKeys){
             Element element = cache.get(cacheKey);
             if (element == null) {
                 // call target/sub-interceptor
                 result = jp.proceed();
                 // 下面方法执行后,将cacheKey与数据集连起来,cacheKey是用来标识这个element的标志,我们可以有多个element(各自是来自不同的数据访问方法而形成的),区分它们就是用cacheKey,
                 element = new Element(cacheKey, (Serializable) result);// 这里的新生成后的element,含有cacheKey,还在element创建时间,访问时间,还有命令次数等cache的属性,我觉得它就像是一个小cache一样,下次要不要更新它就要看它的这些属性来决定。
                 cache.put(element);// 放入cache中
             }
             ddsInstances.add(element.getObjectValue());
         }
         return ddsInstances;
     }
     private List<String>  getCacheKeys(String targetName, String methodName, Object[] arguments) {
         
         List<String> keys = new ArrayList<String>();
         StringBuffer sb = new StringBuffer();
         sb.append(targetName).append(".").append(methodName);
         String key = sb.toString();
         if ((arguments != null) && (arguments.length != 0)) {
             for (int i = 0; i < arguments.length; i++) {
                 keys.add(key+"."+arguments[i]);
             }
         }
         return keys;
     }
    public static void main(String[] args) {
        Serializable [] ids ={"28871"};
        new WcmDdtPublishServiceAspect().getCacheKey("targetName", "getList", ids);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值