I read https://www.baeldung.com/java-instrumentation and I'm trying to instrument the doService
method in org.springframework.web.servlet.DispatcherServlet
, which is part of the Spring Boot framework, ie spring-webmvc.jar
.
This is the way I start my spring boot application:
java -javaagent:agent.jar -jar myapp.jar
I understand my agent's pom.xml
should package/include the javassist
dependency but I don't expect the agent should package/include the jar spring-webmvc.jar
containing the class org.springframework.web.servlet.DispatcherServlet
, as it is already packaged and contained in myapp.jar
In the premain
of the agent, when I try to instrument and load the class org.springframework.web.servlet.DispatcherServlet
by using forName
, it throws the ClassNotFoundException
.
How should I instruct the agent to load it from the myapp.jar
?
Thank you!
try {
targetCls = Class.forName(className);
targetClassLoader = targetCls.getClassLoader();
LOGGER.log(Level.INFO, "Successfully obtain target class {0} and its classloader using 'forName' with class loader {1}", new Object[] {className, targetClassLoader.toString()});
} catch (Exception ex) {
LOGGER.log(Level.SEVERE, "Class {0} not found with Class.forName. {1}", new Object[] {className, ex.getClass().getCanonicalName()});
}
If you are new to Java instrumentation and following this post , note that manually invoking forName
is not necessary in the example. What you can do is to register the transformer in premain
and then in transform
method, check for your target class name and start the instrumentation as instructed in the link. Happy coding!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.