标签:
Spring AOP使用动态代理技术在运行期织入增强的代码,两种代理机制包括:一是基于JDK的动态代理,另一种是基于CGLib的动态代理。之所以需要两种代理机制,很大程度上是因为JDK本身只提供接口的代理,而不支持类的代理。
package com.yyq.proxy; public interface ForumService { void removeTopic(int topicId); void removeForum(int forumId); }
ForumServiceImpl:实现类
package com.yyq.proxy; public class ForumServiceImpl implements ForumService { @Override public void removeTopic(int topicId) { PerformanceMonitor.begin("com.yyq.proxy.ForumServiceImpl.removeTopic"); System.out.println("模拟删除Topic记录:" + topicId); try { Thread.currentThread().sleep(20); } catch (Exception e) { throw new RuntimeException(e); } PerformanceMonitor.end(); } @Override public void removeForum(int forumId) { PerformanceMonitor.begin("com.yyq.proxy.ForumServiceImpl.removeForum"); System.out.println("模拟删除Forum记录:" + forumId); try { Thread.currentThread().sleep(40); }catch (Exception e){ throw new RuntimeException(e); } PerformanceMonitor.end(); } }
PerformanceMonitor:性能监视的实现类
package com.yyq.proxy; public class PerformanceMonitor { private static ThreadLocal<MethodPerformance> performanceRecord = new ThreadLocal<MethodPerformance>(); public static void begin(String method) { System.out.println("begin monitor..."); MethodPerformance mp = new MethodPerformance(method); performanceRecord.set(mp); } public static void end(){ System.out.println("end monitor..."); MethodPerformance mp = performanceRecord.get(); mp.printPerformance(); } }
MethodPerformance:记录性能监视信息
package com.yyq.proxy; public class MethodPerformance { private long begin; private long end; private String serviceMethod; public MethodPerformance(String serviceMethod){ this.serviceMethod = serviceMethod; this.begin = System.currentTimeMillis(); } public void printPerformance(){ end = System.currentTimeMillis(); long elapse = end - begin; System.out.println(serviceMethod + "花费" + elapse + "毫秒。"); } }
TestProxy.testForumService测试方法:
@Test public void testForumService(){ ForumService forumService = new ForumServiceImpl(); forumService.removeForum(10); forumService.removeTopic(1012); }
package com.yyq.proxy; public class ForumServiceImpl implements ForumService { public void removeTopic(int topicId) { System.out.println("模拟删除Topic记录:" + topicId); try { Thread.currentThread().sleep(20); } catch (Exception e) { throw new RuntimeException(e); } PerformanceMonitor.end(); } public void removeForum(int forumId) { System.out.println("模拟删除Forum记录:" + forumId); try { Thread.currentThread().sleep(40); }catch (Exception e){ throw new RuntimeException(e); } PerformanceMonitor.end(); } }
PerformanceHandler:性能监视横切代码
package com.yyq.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class PerformanceHandler implements InvocationHandler { private Object target; public PerformanceHandler(Object target) { this.target = target; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { PerformanceMonitor.begin(target.getClass().getName() + "." + method.getName()); Object obj = method.invoke(target, args); PerformanceMonitor.end(); return obj; } }
@Test public void testForumService2(){ ForumService target = new ForumServiceImpl(); PerformanceHandler handler = new PerformanceHandler(target); ForumService proxy = (ForumService) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),handler); proxy.removeTopic(10); proxy.removeForum(1012); }
package com.yyq.proxy; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; import java.lang.reflect.Method; public class CglibProxy implements MethodInterceptor { private Enhancer enhancer = new Enhancer(); public Object getProxy(Class clazz) { enhancer.setSuperclass(clazz); //设置需要创建子类的 enhancer.setCallback(this); return enhancer.create(); //通过字节码技术动态创建子类实例 } public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { //拦截父类所有方法的调用 PerformanceMonitor.begin(obj.getClass().getName() + "." + method.getName()); Object result = proxy.invokeSuper(obj, args); //通过代理类调用父类中的方法 PerformanceMonitor.end(); return result; } }
@Test public void testForumService3(){ CglibProxy proxy = new CglibProxy(); ForumServiceImpl forumService = (ForumServiceImpl)proxy.getProxy(ForumServiceImpl.class); forumService.removeForum(10); forumService.removeTopic(1012); }
标签:
原文地址:http://www.cnblogs.com/yangyquin/p/5462773.html