简体   繁体   English

Bytebuddy javaagent拦截带有* .Controller批注的spring bean

[英]Bytebuddy javaagent intercepting spring beans annotated with *.Controller

As the title suggests, I'm making a javaagent where the main purpose is to make a nice logger for any spring boot application; 顾名思义,我正在制作一个javaagent,其主要目的是为任何spring boot应用程序创建一个不错的记录器。 for now. 目前。

What I do at the moment is typically: 我目前的工作通常是:

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);
}

Which works really well if I know the path of the class, ie, "org.springframework.web.servlet.DispatcherServlet", and method "doDispatch". 如果我知道该类的路径,即“ org.springframework.web.servlet.DispatcherServlet”和方法“ doDispatch”,该方法就很好用。

Now I'd want to only have the advice added to types annotated with "org.springframework.web.bind.annotation.RestController" without having spring dependency in my agent; 现在,我只想将建议添加到以“ org.springframework.web.bind.annotation.RestController”注释的类型,而在我的代理中没有spring依赖; how can this be done? 如何才能做到这一点?

I've tried 我试过了

..ElementMatchers.annotationType(ElementMatchers.named("org.springframework.web.bind.annotation.RestController");

which doesn't work. 这不起作用。

You can use: 您可以使用:

isAnnotatedWith(named("org.springframework.web.bind.annotation.RestController"))

You are currently matching on a full annotation, not an annotation type. 您当前正在匹配完整注释,而不是注释类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM