有时,从建议中访问处理程序属性很有用。 例如,大多数处理程序都实现允许您访问组件名称。NamedComponentSpring中文文档

可以通过参数(子类化时)或(实现时)访问目标对象。targetAbstractRequestHandlerAdviceinvocation.getThis()org.aopalliance.intercept.MethodInterceptorSpring中文文档

当建议整个处理程序时(例如,当处理程序不生成回复或建议实现时),可以将目标对象强制转换为接口,例如 ,如以下示例所示:HandleMessageAdviceNamedComponentSpring中文文档

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

直接实现时,可以按如下方式强制转换目标对象:MethodInterceptorSpring中文文档

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

当仅建议该方法时(在生成回复的处理程序中),您需要访问完整的处理程序,即 . 以下示例演示如何执行此操作:handleRequestMessage()AbstractReplyProducingMessageHandlerSpring中文文档

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

String componentName = handler.getComponentName();