假设我想将 应用于@Advice.OnMethodEnter中声明的方法org.springframework.web.context.support.GenericWebApplicationContext。为此,我编写了这个最小代理:
public class SequenceAgent {
public static void premain(final String args,
final Instrumentation instrumentation) {
new AgentBuilder.Default()
.with(new AgentBuilder.InitializationStrategy.SelfInjection.Eager())
.type(nameStartsWith(
"org.springframework.web.context.support.GenericWebApplicationContext"))
.transform((builder, typeDescription, classLoader, module) -> builder
.method(any()).intercept(Advice.to(SequenceAdvice.class)))
.installOn(instrumentation);
}
public static class SequenceAdvice {
@Advice.OnMethodEnter
static void enter(@Advice.This Object thiz, @Advice.Origin Method method,
@Advice.AllArguments Object... args) {
String className = thiz.getClass().getName();
String methodName = method.getName();
System.out.println("Entered: " + className + "#" + methodName);
}
}
}
我希望此配置被过滤掉org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext,因为它不匹配org.springframework.web.context.support.GenericWebApplicationContext,但看起来对此类对象的方法调用也被拦截:
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
public class AgentTest {
public static void main(String[] args) {
AnnotationConfigServletWebServerApplicationContext context =
new AnnotationConfigServletWebServerApplicationContext();
context.containsBean("SomeBean");
}
}
当附加到代理并运行时,它会打印出来(为了便于阅读而包装):
Entered: org.springframework.boot.web.servlet.context.
AnnotationConfigServletWebServerApplicationContext
#getResourcePatternResolver
.
.
.
Entered: org.springframework.boot.web.servlet.context.
AnnotationConfigServletWebServerApplicationContext
#getResourceCache
一只甜甜圈
相关分类