案例:
public interface forumservice { void removetopic(int topicid); void removeforum(int forumid); }
对相关方法进行性能监控
public class forumserviceimpl implements forumservice { public void removetopic(int topicid) { // performancemonitor.begin("com.hand.proxy.forumserviceimpl.removetopic"); system.out.println("模拟删除topic记录:" + topicid); try { thread.sleep(20); } catch (interruptedexception e) { e.printstacktrace(); } // performancemonitor.end(); } public void removeforum(int forumid) { // performancemonitor.begin("com.hand.proxy.forumserviceimpl.removeforum"); system.out.println("模拟删除forum记录:" + forumid); try { thread.sleep(20); } catch (interruptedexception e) { e.printstacktrace(); } // performancemonitor.end(); } }
性能监控实现类:
public class performancemonitor { // 通过一个threadlocal保存与调用线程相关的性能监视信息 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(); } }
用于记录性能监控信息:
public class performancemonitor { // 通过一个threadlocal保存与调用线程相关的性能监视信息 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(); } }
1、jdk动态代理
public class performancemonitor { // 通过一个threadlocal保存与调用线程相关的性能监视信息 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(); } }
public class forumservicetest { @test public void proxy() { forumservice forumservice = new forumserviceimpl(); performancehandler handler = new performancehandler(forumservice); forumservice proxy = (forumservice) proxy.newproxyinstance(forumservice.getclass().getclassloader(), forumservice.getclass().getinterfaces(), handler); proxy.removeforum(10); proxy.removetopic(1012); } }
得到以下输出信息:
begin monitor...
模拟删除forum记录:10
end monitor...
com.hand.proxy.forumserviceimpl.removeforum花费21毫秒
begin monitor...
模拟删除topic记录:1012
end monitor...
com.hand.proxy.forumserviceimpl.removetopic花费21毫秒
2、cglib动态代理
<!-- https://mvnrepository.com/artifact/cglib/cglib --> <dependency> <groupid>cglib</groupid> <artifactid>cglib</artifactid> <version>2.2.2</version> </dependency>
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; } }
public class forumservicetest2 { @test public void proxy() { cglibproxy proxy = new cglibproxy(); forumserviceimpl forumservice = (forumserviceimpl) proxy.getproxy(forumserviceimpl.class); forumservice.removeforum(10); forumservice.removetopic(1023); } }
1)、jdk和cglib的区别
2)、spring在选择用jdk还是cglib的依据
3)、jdk和cglib的性能对比
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。如果你想了解更多相关内容请查看下面相关链接
如对本文有疑问, 点击进行留言回复!!
Algebra:Chapter 0 - 预备知识: 集合论和categories
springboot + vue上传图片在服务器并实现在线预览
请谨慎使用增强for循环,刚接触Java值得一看(手动狗头)
【Nginx】还不会使用Nginx解决跨域问题?肝这一篇就够了!!
网友评论