Closed WestFarmer closed 3 years ago
Your Java agent is loaded by the system class loader by default (sun.misc.Launcher$AppClassLoade
) which might be a different class loader than that loading javax.servlet.ServletContainerInitializer
. As a result, you get this exception. The instrumented code will delegate via MethodDelegation
to your ConnectInterceptor
which cannot see the type. Rather use Advice
which inlines code such that it's on the same loader.
When communicating back to your agent, you still need to make sure that your agent code can be seen by the instrumented code. The only reliable approach to this is to inject some code into the boot loader.
@raphw sorry for late reply, can you enlight me with some code
Rather use Advice which inlines code such that it's on the same loader
turn on tomcat class loading log, I see this:
13-Sep-2021 17:18:54.028 详细 [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.findResources findResources(META-INF/services/javax.servlet.ServletContainerInitializer)
13-Sep-2021 17:18:54.030 详细 [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.loadClass loadClass(ch.qos.logback.classic.servlet.LogbackServletContainerInitializer, false)
13-Sep-2021 17:18:54.030 详细 [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.loadClass Delegating to parent classloader1 java.net.URLClassLoader@5579bb86
my agent is using logback which has a META-INF/services/javax.servlet.ServletContainerInitializer
, this cause tomcat to load javax.servlet.ServletContainerInitializer
Within your advice class, you can refer to the classes that you'd expect in your application such as Servlet
. But you cannot guarantee that this class is visible outside of your advice. If you wanted to extract information from your advice, you would need to do so in form of types that are available outside of the class loader, such as String
. If you wanted to inject utility classes, Byte Buddy offers ClassInjector
to add classes to the appropriate class loader.
@raphw I can't find any example of ClassInjector
in bytebuddy's documentation, can you share some example code?
And my agent is using slf4j which is confilcting with application dependencies, what's the common way to solve such issues?
you talk at another level, I can't catch you... sorry.
Have a look at the unit tests.
bytebuddy version 1.11.15 tomcat version : 8.5.64 JAVA version 1.8.0_271
running with:
my agent class: