使用byte-buddy作为agent监听项目方法调用链路出现java.lang.NoSuchMethodError

329 views
Skip to first unread message

Songlin Pan

unread,
Dec 14, 2021, 4:46:22 AM12/14/21
to Byte Buddy
new AgentBuilder.Default()
.with(new AgentBuilder.InitializationStrategy.SelfInjection.Eager())
.ignore(agent.buildExcludeMatcher())
// 过滤
.type(agent.buildIncludeMatcher())
.transform((builder, typeDescription, classLoader, javaModule) -> builder
.method(agent.buildExcludeMethodMatcher())
.intercept(Advice.to(LinkAdvice.class)))
.installOn(instrumentation);


public class LinkAdvice {

@Advice.OnMethodEnter()
public static void enter(@Advice.Origin Method method, @Advice.AllArguments Object[] allArguments) {
// 创建记录
}

@Advice.OnMethodExit()
public static void exit(@Advice.Origin Method method, @Advice.AllArguments Object[] allArguments) {
// 方法结束 将此方法设置到上级方法中生成调用链路
System.out.println("调用链路(MQ) - class: " + method.getDeclaringClass() + " method: " + method.getName());
}
}

我的另一个Sprongboot的项目存在一个mapper
public interface StatisticsSqlMapper {

InterfaceStatisticsDTO getInterfaceStatistics();
}
实现在xml中,当我使用agent代理的时候调用这个方法就会抛出NoSuchMethodError异常,当我不使用的时候就访问正常 项目中使用的mybatis-plus。
然后我尝试过StatisticsSqlMapper extends BaseMapper 使用agent代理后可以正常访问。我就很纳闷这是什么原因 新手入行

Rafael Winterhalter

unread,
Dec 14, 2021, 10:16:14 AM12/14/21
to Songlin Pan, Byte Buddy
Sorry, I do not speak Chinese and cannot understand your question.

--
You received this message because you are subscribed to the Google Groups "Byte Buddy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to byte-buddy+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/byte-buddy/e7d1cab2-fc9c-4f3b-9496-3c1abeb7e26fn%40googlegroups.com.

Songlin Pan

unread,
Dec 14, 2021, 10:00:50 PM12/14/21
to Byte Buddy
I have a Springboot project that introduces mybatis- Plus dependencies. When I have a query of SQL interface class inherited basemapper start projects to join byte - budd as agent when query error in Java. Lang. NoSuchMethodError. It works fine when I remove agent or when I inherit the interface class from Basemapper

Rafael Winterhalter

unread,
Dec 15, 2021, 5:21:29 AM12/15/21
to Songlin Pan, Byte Buddy
A Java agent is always added to the system class loader. Classes that are instrumented might end up on different class loaders. You would need to make sure your agent is visible to instrumented classes if you are calling back. Normally, you would add some code to the bootstrap class loader which is shared among all classes in a VM.

The Instrumentation instance that is supplied offers a way of injecting classes. Byte Buddy also offers facilities via ClassInjector.

Reply all
Reply to author
Forward
0 new messages