是的,又是这个问题...
我设计了一个批处理系统,它调用 ActionService 的通知方法,在通知方法内,根据调用方式,调用创建批处理服务的方法。
示例代码块如下所示;
@Service
public class BatchService{
private final BatchRepository batchRepository;
private final ActionService actionService;
public Batch create(Batch batch){
return batchRepository.save(batch);
}
public void triggerNotifiers(){
...
actionService.notify(...);
...
}
}
@Service
public class ActionService{
private final ActionRepository actionRepository;
private final BatchService batchService;
public void notify(...){
...
if(/*some special cases*/)
batchService.create(...);
...
}
}
批处理服务的功能是调用Action服务的notify方法。 操作服务可以根据传入的通知数据创建 future 的通知程序。
所以他们非常依赖彼此。
您认为是否有更好的方法/逻辑/设计来做到这一点?
最佳答案
在写题的时候,我想出了一个办法:
@Component
public class Batch{
private final BatchService batchService;
private final ActionService actionService;
public void triggerNotifiers(){
...
batchService.findProperBatch(...);
actionService.notify(...);
...
}
}
@Service
public class BatchService{
private final BatchRepository batchRepository;
public Batch create(Batch batch){
return batchRepository.save(batch);
}
public Batch findProperBatch(...){
...
return batch;
}
}
@Service
public class ActionService{
private final ActionRepository actionRepository;
private final BatchService batchService;
public void notify(...){
...
if(/*some special cases*/)
batchService.create(...);
...
}
}
如果您有更好/替代的解决方案,请不要犹豫,分享您的想法。
关于java - spring boot循环依赖设计问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62233522/