Annotation实现缓存,使用AOP的HotSwappableTargetSource
HotSwappableTargetSource是一个可以动态替换的对象,这个类很有用,可以在运行期间动态替换目标对象。
如下配置:
新建一个ehcache的动态代理替换类/** * 在当前Factory的都会cache. */ApplicationContext cxt = new ClassPathXmlApplicationContext(new String[]{"annotation/ano.xml","annotation/bean.xml"});//TestI testI = (TestI) cxt.getBean("testI");TestI testI = (TestI)new EhCacheDynaProxy<Intabc>(cxt,"&dynaProxy","testI").getObject(); int a = 10;int b = 11;System.out.println(testI.add(a, b));System.out.println(testI.add(a, b));System.out.println(testI.xx(a));System.out.println(testI.xx(a)); Intabc intabc = new EhCacheDynaProxy<Intabc>(cxt,"&dynaProxy","intabc").getObject(); System.out.println(intabc.test(a, b));System.out.println(intabc.test(a, b));
可以看到EhCacheDynaProxy可以代理任何接口类,并动态的生成Ehcache代理对象,实现缓存.