该版本仍在开发中,尚未被视为稳定。对于最新的稳定版本,请使用 Spring Integration 7.0.0spring-doc.cadn.net.cn

自定义建议课程

除了前面提到的建议类外,你还可以自己实施建议类。 虽然你可以提供任何实现org.aopalliance.aop.Advice(通常org.aopalliance.intercept.方法拦截者),我们通常建议你进行子职业o.s.i.handler.advice.摘要请求HandlerAdvice. 这不仅避免了编写面向切面的底层编程代码,还提供了一个专门为该环境量身定制的起点。spring-doc.cadn.net.cn

子类需要实现doInvoke()方法的定义如下:spring-doc.cadn.net.cn

/**
 * Subclasses implement this method to apply behavior to the {@link MessageHandler} callback.execute()
 * invokes the handler method and returns its result, or null).
 * @param callback Subclasses invoke the execute() method on this interface to invoke the handler method.
 * @param target The target handler.
 * @param message The message that will be sent to the handler.
 * @return the result after invoking the {@link MessageHandler}.
 * @throws Exception
 */
protected abstract Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception;

回调参数是为了方便避免直接处理AOP的子类。 引用callback.execute()方法调用消息处理器。spring-doc.cadn.net.cn

目标参数为需要为特定处理程序保持状态的子类提供,可能通过在地图由目标锁定。 该功能允许将相同的建议应用于多个处理器。 这请求处理者断路器建议使用建议来保持每个处理器的断路器状态。spring-doc.cadn.net.cn

消息参数是发送给处理器的消息。 虽然建议不能在调用处理程序前修改消息,但可以修改有效载荷(如果其具有可变属性)。 通常,建议会将消息用于日志记录,或在调用处理器之前或之后发送消息副本。spring-doc.cadn.net.cn

返回值通常为callback.execute(). 不过,该建议确实有能力修改返回价值。 注意摘要回复制作消息处理器实例返回值。 以下示例展示了一个自定义建议类,扩展为摘要请求处理员建议:spring-doc.cadn.net.cn

public class MyAdvice extends AbstractRequestHandlerAdvice {

    @Override
    protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
        // add code before the invocation
        Object result = callback.execute();
        // add code after the invocation
        return result;
    }
}

此外执行()方法执行回调提供了一种额外的方法:cloneAndExecute(). 当调用在一次执行中可能被多次调用时,必须使用此方法。doInvoke(),例如在请求处理员再试建议. 这是因为春季AOP是必须的org.springframework.aop.framework.ReflectiveMethodInvocation对象通过跟踪链中最后调用的建议来维持状态。 每次通话都必须重置该状态。spring-doc.cadn.net.cn

更多信息请参见 ReflectiveMethodInvocation Javadoc。spring-doc.cadn.net.cn