gpt4 book ai didi

java - 多方法拦截和拦截器复用

转载 作者:行者123 更新时间:2023-12-02 13:22:08 24 4
gpt4 key购买 nike

这是我的用例:我有一个类,它有多个我想要拦截的方法,但我不想拦截该类的所有方法。我想使用同一拦截器类的不同实例来完成此任务。当我尝试执行此操作时,我从 Byte Buddy 看到了我无法理解的行为。我很确定这是我不理解的事情和/或我做错的事情,但我很难过。我正在使用 Byte Buddy 1.6.9。

我想要拦截其方法的类:

public class MyClass
{
private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

public void firstInterceptedMethod()
{
logger.info("firstInterceptedMethod");
}

public void secondInterceptedMethod()
{
logger.info("secondInterceptedMethod");
}

public void notInterceptedMethod()
{
logger.info("notInterceptedMethod");
}
}

拦截器类:

public class MyInterceptor
{
private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

private final UUID identifier = UUID.randomUUID();

@RuntimeType
public Object methodCalled(@SuperCall Callable<?> superCall, @Origin Method method) throws Exception
{
logger.info("methodCalled: identifier: " + identifier);
logger.info("methodCalled: method name: " + method.getName());

return superCall.call();
}

}

Byte Buddy 测试/检测:

public class MyTest
{
@Test
public void test() throws Exception
{
MyInterceptor firstMethodInterceptor = new MyInterceptor();
MyInterceptor secondMethodInterceptor = new MyInterceptor();

MyClass myClass = new ByteBuddy().subclass(MyClass.class)
.method(ElementMatchers.named("firstInterceptedMethod"))
.intercept(MethodDelegation.to(firstMethodInterceptor))
.method(ElementMatchers.named("secondInterceptedMethod"))
.intercept(MethodDelegation.to(secondMethodInterceptor))
.make()
.load(MyClass.class.getClassLoader())
.getLoaded()
.newInstance();

myClass.firstInterceptedMethod();
myClass.secondInterceptedMethod();
}
}

输出:

04/20/2017 08:27:26:600 AM [main] bytebuddy.test.MyInterceptor methodCalled : INFO methodCalled: identifier: 04124951-f865-4815-8bd4-0b10c0c816a2
04/20/2017 08:27:26:600 AM [main] bytebuddy.test.MyInterceptor methodCalled : INFO methodCalled: identifier: 04124951-f865-4815-8bd4-0b10c0c816a2
04/20/2017 08:27:26:613 AM [main] bytebuddy.test.MyInterceptor methodCalled : INFO methodCalled: method name: firstInterceptedMethod
04/20/2017 08:27:26:613 AM [main] bytebuddy.test.MyInterceptor methodCalled : INFO methodCalled: method name: firstInterceptedMethod
04/20/2017 08:27:26:614 AM [main] bytebuddy.test.MyClass firstInterceptedMethod : INFO firstInterceptedMethod
04/20/2017 08:27:26:614 AM [main] bytebuddy.test.MyInterceptor methodCalled : INFO methodCalled: identifier: 79590462-b87d-4125-9e87-5481e1062b05
04/20/2017 08:27:26:614 AM [main] bytebuddy.test.MyInterceptor methodCalled : INFO methodCalled: identifier: 79590462-b87d-4125-9e87-5481e1062b05
04/20/2017 08:27:26:614 AM [main] bytebuddy.test.MyInterceptor methodCalled : INFO methodCalled: method name: secondInterceptedMethod
04/20/2017 08:27:26:614 AM [main] bytebuddy.test.MyInterceptor methodCalled : INFO methodCalled: method name: secondInterceptedMethod
04/20/2017 08:27:26:614 AM [main] bytebuddy.test.MyClass secondInterceptedMethod : INFO secondInterceptedMethod

我的问题是:为什么我看到每个拦截器被调用两次?当调用第一个方法时,第一个拦截器实例似乎被调用了两次,我不明白这是为什么。与第二个方法的行为类似,只有第二个拦截器实例似乎被调用了两次。我希望拦截器每次拦截的方法调用只被调用一次。

如果我做类似的事情:

    FirstMethodInterceptor firstMethodInterceptor = new FirstMethodInterceptor();
SecondMethodInterceptor secondMethodInterceptor = new SecondMethodInterceptor();

然后我看到了我期望的行为;即,每个拦截器对于每个被拦截的方法仅被调用一次。我希望避免这种情况,因为我希望能够重用“通用”拦截器而不会看到这种行为。非常感谢您的见解,谢谢。

最佳答案

是的,像往常一样,这是我做错的事情。这是记录器的产物,而不是 Byte Buddy 的产物。正在为同一个记录器实例注册多个日志记录处理程序,这就是导致此问题的原因。

关于java - 多方法拦截和拦截器复用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43520125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com