Home The dependencies of some of the beans in the application context form a cycle:
Post
Cancel

The dependencies of some of the beans in the application context form a cycle:

Error

  • rest template을 다음과 같이 사용했을 때 Error가 발생했다.
  • service 인터페이스

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
      package kr.pe.playdata.service;
        
      import java.util.concurrent.CompletableFuture;
        
      public interface RecommandService {
          /*
              관광지 추천 서비스 인터페이스
           */
        
          public CompletableFuture<String> recommand(String keywords);
      }
    
  • service 구현

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    
      package kr.pe.playdata.service.impl;
        
      import kr.pe.playdata.service.RecommandService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.scheduling.annotation.Async;
      import org.springframework.stereotype.Service;
      import org.springframework.util.LinkedMultiValueMap;
      import org.springframework.util.MultiValueMap;
      import org.springframework.web.client.RestTemplate;
      import java.util.concurrent.CompletableFuture;
        
      @Service
      public class RecommandServiceImpl implements RecommandService {
          /*
              관광지를 추천해주는 서비스 구현체
           */
        
          @Autowired
          private RestTemplate restTemplate;
        
      		@Bean
          public RestTemplate restTemplate(){
              return new RestTemplate();
          }
        
          @Async("asyncExecutor")
          public CompletableFuture<String> recommand(String keywords){
              String url = "http://127.0.0.1:5000/recommand";
              MultiValueMap<String, String> param = new LinkedMultiValueMap<String, String>();
              param.add("keywords",keywords);
              String sb = restTemplate.postForObject(url, param, String.class);
              return CompletableFuture.completedFuture(sb);
          }
      }
    
  • Error 발생

    스크린샷_2022-04-06_오후_8 34 10

Error 해결

  • RestTemplate Config를 만들어 RestTemplate을 미리 Bean에 등록하였다.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
      package kr.pe.playdata.config;
        
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.web.client.RestTemplate;
        
      @Configuration
      public class RestTemplateConfig {
          /*
              rest Config
           */
        
          @Bean
          public RestTemplate restTemplate(){
              return new RestTemplate();
          }
      }
    

참고 사이트

Circular Dependency in Spring Boot

This post is licensed under CC BY 4.0 by the author.