拦截方法:
1、普通方法 @OnMethod(clazz="", method="")
2、构造函数@OnMethod(claszz="", method="<init>")
3、拦截同名函数,用参数区分
一、拦截构造函数
1、创建接口
@RequestMapping("/constructor") public User constructor(User user){ return user; }
2、测试接口
http://127.0.0.1:8080/monitor_tuning/ch4/constructor?id=1&name=nick
3、查看进程
4、创建btrace脚本,运行btrace脚本
package com.example.monitor_tuning.chapter4; import com.sun.btrace.AnyType; import com.sun.btrace.BTraceUtils; import com.sun.btrace.annotations.*; /** * 拦截构造函数. */ @BTrace public class PrintConstructor { /*要拦截哪个类,<init>w为构造函数,*/ @OnMethod( clazz = "com.example.monitor_tuning.chapter2.User", method="<init>" ) /*ProbeClassName 方法类名; ProbeMethodName 方法名 ; AnyType[] 构造函数参数*/ public static void anyRead(@ProbeClassName String pcn, @ProbeMethodName String pmn, AnyType[] args) { BTraceUtils.printArray(args); BTraceUtils.println(pcn + "," + pmn); BTraceUtils.println(); } }
运行btrace脚本
显示结果
二、拦截同名函数
1、创建两个同名的方法
@RequestMapping("/same1") public String same(@RequestParam("name")String name){ return "hello," + name; } @RequestMapping("/same2") public String same(@RequestParam("id")int id, @RequestParam("name")String name){ return "hello," + name + " id: " + id; }
2、创建btrace脚本
通过参数个数,和顺序进行匹配。
package com.example.monitor_tuning.chapter4; import com.sun.btrace.BTraceUtils; import com.sun.btrace.annotations.*; /** * 监控同名方法 */ @BTrace public class PrintSame { @OnMethod( clazz = "com.example.monitor_tuning.chapter4.Ch4Controller", method="same" ) public static void anyRead(@ProbeClassName String pcn, @ProbeMethodName String pmn, int id, String name) { BTraceUtils.println(pcn + "," + pmn + "," + name + "," + id); BTraceUtils.println(); } }
3、运行btrace脚本
调用接口
http://127.0.0.1:8080/monitor_tuning/ch4/same2?name=nick&id=2
最终显示结果见上图