我正在开发一个spring 2.0.1.RELEASE应用程序。
申请摘要:
1. I have separate Transformer beans that transforms my DTO to Domain
and vice versa.
2. I have separate Validator beans that validate my domain object being passed.
3. I have Service classes that takes care of the applying rules and calling persistence layer.
现在,我想在我的应用程序中构建工作流程: 我将在其中调用工作流程的开始,下面提到的步骤将按顺序执行,异常处理将按照以下步骤进行:
1.First-Transformtion - transformToDomain() method will be called for that object type.
2.Second-Validator - class valid() method will be called for that object.
3.Third-Service - class save() method will be called for that object.
4.Fourth- Transformation - transformToDTO() method will be called for that object type.
在此之后我的工作流程结束,我将返回DTO对象作为我的REST API的响应。
异常处理部分是一个,我也想要处理,就像该步骤存在特定的异常处理程序然后调用它,否则调用全局异常处理程序。
我设计了一些相同的原型,但寻找一些专家建议,以及如何通过更好的java设计实现这一点。
考虑上述用例的示例解释非常明显。
答案 0 :(得分:0)
简短我以一种没有太多喧嚣的方式实施的内容:
Stream.<Supplier<RequestHandler>>of(
TransformToDomainRequestHandler::new,
ValidateRequestHandler::new,
PersistenceHandler::new,
TransformToDTORequestHandler::new)
.sequential()
.map(c -> c.get()) /* Create the handler instance */
.reduce((processed, unProcessed) -> { /* chains all handlers together */
RequestHandler previous = processed;
RequestHandler target = previous.getNextRequestHandler();
while (target != null && previous != null) {
previous = target;
target = target.getNextRequestHandler();
}
previous.setNextRequestHandler(unProcessed);
return processed;
}).get();
这是我的请求处理程序,其他所有处理程序都扩展了