ClassLoader, JavaAgent, Aspectj Weaving

本文深入探讨了Java ClassLoader的工作原理及其实现方式,并详细解析了Java Agent技术的应用场景及其内部工作机制。通过具体实例展示了如何利用这些技术进行字节码增强等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://calvin1978.blogcn.com/articles/classloader-javaagent.html
在做 javaagent 时 ,使用 agentMain 对已经运行中的 springboot 应用中的 restController 的 postmapping 修饰的方法 做切面,注意这里是 agentMain ,以下方法执行之前大部分类已经加载结束并且对应的controller生成了单例的bean,package org.sunyaxing.transflow.agent.agentplugin; import net.bytebuddy.agent.ByteBuddyAgent; import net.bytebuddy.agent.builder.AgentBuilder; import net.bytebuddy.implementation.MethodDelegation; import net.bytebuddy.matcher.ElementMatchers; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.lang.instrument.Instrumentation; public class TransflowAgent { private static final Logger log = LoggerFactory.getLogger(TransflowAgent.class); public static void agentmain(String agentArgs, Instrumentation instrumentation) { ByteBuddyAgent.install(); new AgentBuilder.Default() .with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION) .with(AgentBuilder.TypeStrategy.Default.REDEFINE) .type(ElementMatchers.isAnnotatedWith(RestController.class)) .transform((builder, typeDescription, classLoader, javaModule) -> { log.info("agentmain transform class {} {}", typeDescription, classLoader); return builder .method( ElementMatchers.isAnnotatedWith(RequestMapping.class) .or(ElementMatchers.isAnnotatedWith(GetMapping.class)) .or(ElementMatchers.isAnnotatedWith(PostMapping.class)) ) .intercept(MethodDelegation.to(MonitorInterceptor.class)); }).installOnByteBuddyAgent(); } 然而,注入对已经运行的serverA 注入agent后,调用postmapping所修饰的方法并没有触发切面,是什么原因,如何解决
04-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值