5

Java agent 入门遇到点问题

 1 year ago
source link: https://www.v2ex.com/t/943064
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  Java

Java agent 入门遇到点问题

  moxiaowei · 4 小时 38 分钟前 · 873 次点击

有一个普通的 springboot 项目,我想对项目中的方法执行时间进行计算。然后用 maven 搭了个 agent ,但是使用 java -javaagent 把探针加进去的时候,提示在探针中找不到对应的类。探针的代码,很简单,就是这样的:

ClassPool classPool = new ClassPool(true);
classPool.insertClassPath(new LoaderClassPath(LoadTimeAgent.class.getClassLoader()));
String targentClassName = "com.example.utils.BitStringUtil";
//根据类名获取到要修改的 class
CtClass targetClass = classPool.get(targentClassName);

......下面的代码省略了......

想想确实,agent 里面没有 com.example.utils.BitStringUtil 这样的类,然后我查了资料,说是 springboot 项目,java -javaagent:agent.jar -jar app.jar 的时候,探针中的 class 文件是被 AppClassLoader 加载的,而 springboot 中的 class 文件是被 LunchedURLClassLoader 加载的,而 LunchedURLClassLoader 是 AppClassLoader 的子类,子类( LunchedURLClassLoader )是无法被父类( AppClassLoader )看到的,所以在探针中是无法获取到 springboot 中的类的,那么有没有什么方法能处理?我想,别的探针项目,比如 sw 也不至于获取不到吧?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK