建议的处理程序属性

有时,从 advice 中访问 handler 属性很有用。 例如,大多数处理程序都实现了NamedComponent以访问组件名称。spring-doc.cadn.net.cn

目标对象可以通过target参数(当子类化AbstractRequestHandlerAdvice) 或invocation.getThis()(实施org.aopalliance.intercept.MethodInterceptor).spring-doc.cadn.net.cn

当整个处理程序都被通知时(例如,当处理程序不生成回复或建议实现HandleMessageAdvice),则可以将目标对象强制转换为接口,例如NamedComponent,如以下示例所示:spring-doc.cadn.net.cn

String componentName = ((NamedComponent) target).getComponentName();

当您实施MethodInterceptor直接转换目标对象,如下所示:spring-doc.cadn.net.cn

String componentName = ((NamedComponent) invocation.getThis()).getComponentName();

当只有handleRequestMessage()方法(在生成回复的处理程序中),您需要访问完整的处理程序,它是一个AbstractReplyProducingMessageHandler. 以下示例显示了如何执行此作:spring-doc.cadn.net.cn

AbstractReplyProducingMessageHandler handler =
    ((AbstractReplyProducingMessageHandler.RequestHandler) target).getAdvisedHandler();

String componentName = handler.getComponentName();