javaspring-batchhexagonal-architecture

How to add an Interface into a constructor in an ItemProcessor with SpringBatch


In my java project I use the hexagonal architecture. I have an Interface "Use Case" called RUseCase who is implemented by a Service called "RService".

In my ItemProcessor of my spring batch step I need to call to my Interface "RUseCase", so in my Processor I put in my constructor the interface thank's to the annotation RequiredArgsConstructor. But I got an error when I try to put the Interface into the parameter of my Processor.

I don't see in the documentation how to do this.. Do you have any ideas ?

In my declaration of processor :

  @Bean
  public ItemProcessor<PSSEntity, PLEntity> rProcessor() {
    return new RProcessor(); <-- Error here
  }

RProcessor :

@Slf4j
@RequiredArgsConstructor
public class RProcessor implements ItemProcessor<PSSEntity, PLEntity> {

  private final RUseCase rUseCase;


  @Override
  public PLEntity process(PSSEntity item) {
    log.trace("Traitement d'une entrée PSSEntity: {}", item);

    List<PL> pL = this.rUseCase.findR(item.getP());
    return new PLEntity();
  }
}

When I tried to put the RUseCase in the contructor of the BatchConfiguration and if I declare the bean like :

  @Bean
  public RUseCase rUseCase(RUseCase rUseCase) {
    return rUseCase;
  }

I got an error : The dependencies of some of the beans in the application context form a cycle:


Solution

  • Your RProcessor requires a RUseCase, you should have a constructor generated by @RequiredArgsConstructor.

    You need to declare a bean of type RUseCase, and then pass it to your item processor like follows for example:

    @Bean
    public ItemProcessor<PSSEntity, PLEntity> rProcessor(RUseCase rUseCase) {
        return new RProcessor(rUseCase);
    }