Bytebuddy javaagent 拦截 spring 用 *.Controller 注释的 beans
Bytebuddy javaagent intercepting spring beans annotated with *.Controller
如标题所示,我正在制作一个 javaagent,其主要目的是为任何 spring 启动应用程序制作一个不错的记录器;现在。
我现在做的通常是:
private static void install(String className, String methoName, Instrumentation instr) {
new AgentBuilder.Default().disableClassFormatChanges()
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.type(ElementMatchers.named(className))
.transform((builder, typeDescription, classLoader, module) -> {
return builder.visit(Advice.to(AdviceDispatcherServlet.class).on(ElementMatchers.named(methoName)));
}).installOn(instr);
}
如果我知道 class 的路径,即 "org.springframework.web.servlet.DispatcherServlet" 和方法 "doDispatch"。
,那么效果会很好
现在我只想将建议添加到用 "org.springframework.web.bind.annotation.RestController" 注释的类型中,而我的代理中没有 spring 依赖项;如何做到这一点?
我试过了
..ElementMatchers.annotationType(ElementMatchers.named("org.springframework.web.bind.annotation.RestController");
这是行不通的。
您可以使用:
isAnnotatedWith(named("org.springframework.web.bind.annotation.RestController"))
您当前匹配的是完整注释,而不是注释类型。
如标题所示,我正在制作一个 javaagent,其主要目的是为任何 spring 启动应用程序制作一个不错的记录器;现在。
我现在做的通常是:
private static void install(String className, String methoName, Instrumentation instr) {
new AgentBuilder.Default().disableClassFormatChanges()
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.type(ElementMatchers.named(className))
.transform((builder, typeDescription, classLoader, module) -> {
return builder.visit(Advice.to(AdviceDispatcherServlet.class).on(ElementMatchers.named(methoName)));
}).installOn(instr);
}
如果我知道 class 的路径,即 "org.springframework.web.servlet.DispatcherServlet" 和方法 "doDispatch"。
,那么效果会很好现在我只想将建议添加到用 "org.springframework.web.bind.annotation.RestController" 注释的类型中,而我的代理中没有 spring 依赖项;如何做到这一点?
我试过了
..ElementMatchers.annotationType(ElementMatchers.named("org.springframework.web.bind.annotation.RestController");
这是行不通的。
您可以使用:
isAnnotatedWith(named("org.springframework.web.bind.annotation.RestController"))
您当前匹配的是完整注释,而不是注释类型。