title: UnsatisfiedDependencyException and Circular Dependency
body: <p>I am trying to make some Spring Boot legacy code build and run, but it fails with:</p>
  <pre><code>org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataLoader' defined in file [......\bootstrap\DataLoader.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'organizationServiceImpl': Unsatisfied dependency expressed through method 'setAssessmentService' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'assessmentServiceImpl': Invocation of init method failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
      
  ......
      
      at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
  Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'organizationServiceImpl': Unsatisfied dependency expressed through method 'setAssessmentService' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'assessmentServiceImpl': Invocation of init method failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:676)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
      at ........
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'assessmentServiceImpl': Invocation of init method failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
      at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:139)
  </code></pre>
  <p>DataLoader class is:</p>
  <pre><code>@Component
  public class DataLoader implements ApplicationRunner {
  
      private final OrganizationService organizationService;
  
      private List&lt;String&gt; organizationNames = Arrays.asList(
          ...some long list of string literals...
  
      );
  
      public DataLoader(OrganizationService organizationService) {
          this.organizationService = organizationService;
      }
  
      @Override
      public void run(ApplicationArguments args) {
          if (CollectionUtils.isEmpty(organizationService.findAll())) {
              addOrganizations();
          }
      }
  
      private void addOrganizations() {
          organizationNames.forEach(name -&gt; {
              OrganizationRequest organization = new OrganizationRequest();
              organization.setName(name);
              if (
              ....irrelevant code....
              }
              organizationService.save(organization);
          });
      }
  
  }
  </code></pre>
  <p>and OrganizationServiceImpl class is:</p>
  <pre><code>@Service
  public class OrganizationServiceImpl implements OrganizationService {
  
      private final OrganizationRepository organizationRepository;
      private final AddressService addressService;
      private final ContactService contactService;
      private final SupplierQualifierService supplierQualifierService;
      private final MonitoringScopeService monitoringScopeService;
      private final FinancialService financialsService;
  
      private DocumentService documentService;
      private AssessmentService assessmentService;
  
      @Getter
      private Map&lt;Long, Set&lt;QuestionType&gt;&gt; questionTypesForAssessmentByOrganization = new HashMap&lt;&gt;();
  
      public OrganizationServiceImpl(OrganizationRepository organizationRepository, AddressService addressService, ContactService contactService, SupplierQualifierService supplierQualifierService, MonitoringScopeService monitoringScopeService, FinancialService financialsService ) {
          this.organizationRepository = organizationRepository;
          this.addressService = addressService;
          this.contactService = contactService;
          this.supplierQualifierService = supplierQualifierService;
          this.monitoringScopeService = monitoringScopeService;
          this.financialsService = financialsService;
      }
  
      //workaround for circular dependency
      @Autowired
      public void setAssessmentService(AssessmentService assessmentService) {
          this.assessmentService = assessmentService;
      }
  
      // to prevent circular dependency problem ...
      @Autowired 
      public void setDocumentService(DocumentService documentService) {
          this.documentService = documentService;
      }
  
      @Override
      public Organization findById(Long id) {
          Optional&lt;Organization&gt; organization = organizationRepository.findById(id);
          return organization.orElse(new Organization());
      }
  
      @Override
      public List&lt;Organization&gt; findAll() {
          return organizationRepository.findAll();
      }
  
      @Override
      public Organization save(OrganizationRequest request) {
      ....long not relevant code...
  
          return savedOrganization;
      }
  
  
      ....long not relevant code, some business logic...
  
  }
  </code></pre>
  <p>AssessmentServiceImpl class is:</p>
  <pre><code>@Service
  public class AssessmentServiceImpl implements AssessmentService {
  
  private final AssessmentRepository assessmentRepository;
  private final OrganizationService organizationService;
  private final AnswerService answerService;
  private final QuestionService questionService;
  
  private Map&lt;QuestionType, Integer&gt; numberOfQuestionsByType = new HashMap&lt;&gt;();
  
  public AssessmentServiceImpl(AssessmentRepository assessmentRepository, OrganizationService organizationService, AnswerService answerService, QuestionService questionService) {
      this.assessmentRepository = assessmentRepository;
      this.organizationService = organizationService;
      this.answerService = answerService;
      this.questionService = questionService;
  }
  
  @PostConstruct
  public void postConstruct() {
      for (QuestionType type : QuestionType.values()) {
          numberOfQuestionsByType.put(type, questionService.findByQuestionType(type).size());
      }
  }
  
  
  ... some business logic code....
  </code></pre>
  <p>}</p>
  <p>Chain of failed dependency injection is:</p>
  <pre><code>Error creating bean with name 'dataLoader':
  Error creating bean with name 'organizationServiceImpl': 
  Error creating bean with name 'assessmentServiceImpl': 
  </code></pre>
  <p>But I don't see direct cause for it.</p>
  <p>I see that original developer ran into cyclic dependency issues, but from what I Googled it seems this should be appropriate solution:</p>
  <pre><code>//workaround for circular dependency
  @Autowired
  public void setAssessmentService(AssessmentService assessmentService) {
     this.assessmentService = assessmentService;
  }
  
  // to prevent circular dependency problem ...
  @Autowired 
  public void setDocumentService(DocumentService documentService) {
      this.documentService = documentService;
  }
  </code></pre>