1.首先配置ehcache.xml

<ehcache>

    <diskStore path="java.io.tmpdir"/>
   
    <defaultCache  
        maxElementsInMemory="10000"    
        eternal="false"                        
        timeToIdleSeconds="120"       
        timeToLiveSeconds="120"   
        overflowToDisk="true"         
    />  

    <cache name="org.taha.cache.METHOD_CACHE"
        maxElementsInMemory="300"
        eternal="false"
        timeToIdleSeconds="60"
        timeToLiveSeconds="60"
        overflowToDisk="true"
        />

</ehcache>

 //defaultCache  一定要有不然解析报错

2. spring-ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans default-autowire="byName">

 <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>org.taha.cache.METHOD_CACHE</value>
  </property>

 </bean>


 <bean id="methodCacheInterceptor"
  class="com.infowarelab.cem.easytouch.web.interceptor.MethodCacheInterceptor">

  <property name="cache">

   <ref local="methodCache" />

  </property>

 </bean>

 

 <bean id="methodCachePointCut"
  class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">

  <property name="advice">

   <ref local="methodCacheInterceptor" />

  </property>

  <property name="patterns">

   <list>

    <value>.*methodOne</value>

    <value>.*methodTwo</value>   //匹配methodTwo的方法    
    <value>.*cache.*</value>      //匹配以 cache开头的方法

   </list>

  </property>

 </bean>

 

 <bean id="myBean"
  class="org.springframework.aop.framework.ProxyFactoryBean">

  <property name="target">

   <bean class="com.infowarelab.cem.easytouch.web.interceptor.MyBean" />

  </property>

  <property name="interceptorNames">

   <list>

    <value>methodCachePointCut</value>

   </list>

  </property>

 </bean>
 
 
 

</beans>


 3 MethodCacheInterceptor 代码

package com.infowarelab.cem.easytouch.web.interceptor;

import java.io.Serializable;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.factory.InitializingBean;

public class MethodCacheInterceptor implements MethodInterceptor,

InitializingBean {

 private Cache cache;

 /**
  *
  * sets cache name to be used
  *
  */

 public void setCache(Cache cache) {

  this.cache = cache;

 }

 /*
  *
  * (non-Javadoc)
  *
  *
  *
  * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
  *
  */

 public Object invoke(MethodInvocation invocation) throws Throwable {

  String targetName = invocation.getThis().getClass().getName();

  String methodName = invocation.getMethod().getName();

  Object[] arguments = invocation.getArguments();

  Object result;

  String cacheKey = getCacheKey(targetName, methodName, arguments);

  Element element = cache.get(cacheKey);

  if (element == null) {

   // call target/sub-interceptor

   result = invocation.proceed();

   // cache method result

   element = new Element(cacheKey, (Serializable) result);

   cache.put(element);

  }

  return element.getValue();

 }

 /**
  *
  * creates cache key: targetName.methodName.argument0.argument1...
  *
  */

 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();

 }

 /*
  *
  * (non-Javadoc)
  *
  *
  *
  * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
  *
  */

 public void afterPropertiesSet() throws Exception {

  // TODO Auto-generated method stub

 }

}


4 MyBean 代码部分

package com.infowarelab.cem.easytouch.web.interceptor;

import com.infowarelab.cem.easytouch.service.ConfigdbInfoService;

public class MyBean {
 
 
 private ConfigdbInfoService configdbInfoService;
 

 public String  methodOne(String siteId,String name){
 
     String cityName=configdbInfoService.getCityNameByIp("192.168.1.92");
    
  return ""+siteId+name+cityName;
 }
 
 public String  methodTwo(String siteId,String name,String pwd){
  
  String cityName=configdbInfoService.getCountryNameByIp("192.168.1.92");
  
  return ""+siteId+name+cityName;
 }

 public void setConfigdbInfoService(ConfigdbInfoService configdbInfoService) {
  this.configdbInfoService = configdbInfoService;
 }
 
 
 
}


5.CopyOfLogin 代码 测试 AOP 代码


package com.infowarelab.cem.easytouch.web.action.api;

import com.infowarelab.cem.easytouch.web.interceptor.MyBean;

/**
 *
 * @author hummer.hu
 * @date:2008-4-24
 *
 */
public class CopyOfLogin extends ApiBaseAction {

 
 
 private MyBean myBean;
 

 public String execute() {

 
  
  myBean.methodOne("5555555", "aaaa");
  myBean.methodTwo("6666666", "aaaa", "bbbb");
  return "login";
  
  
 }
 public void setMyBean(MyBean myBean) {
  this.myBean = myBean;
 }

 
 
}



------君临天下,舍我其谁------