daadaadaah / review-study-app

0 stars 0 forks source link

순환 참조 문제 해결 #73

Open daadaadaah opened 3 months ago

daadaadaah commented 3 months ago

순환 참조 문제 : AppConfig 순환 참조 문제

에러 메시지

Description:

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

   localMethodExecutionTimeLoggingAspect defined in file [/Users/daheekwak/Desktop/review-study-app/out/production/classes/com/example/review_study_app/common/aop/LocalMethodExecutionTimeLoggingAspect.class]
      ↓
   discordNotificationService defined in file [/Users/daheekwak/Desktop/review-study-app/out/production/classes/com/example/review_study_app/service/notification/DiscordNotificationService.class]
      ↓
   restTemplateHttpClient defined in file [/Users/daheekwak/Desktop/review-study-app/out/production/classes/com/example/review_study_app/infrastructure/resttemplate/RestTemplateHttpClient.class]
┌─────┐
|  appConfig defined in file [/Users/daheekwak/Desktop/review-study-app/out/production/classes/com/example/review_study_app/common/config/AppConfig.class]
↑     ↓
|  gitHubApiLoggingInterceptor defined in file [/Users/daheekwak/Desktop/review-study-app/out/production/classes/com/example/review_study_app/infrastructure/resttemplate/interceptor/GitHubApiLoggingInterceptor.class]
↑     ↓
|  logDirectSaveGoogleSheetsService defined in file [/Users/daheekwak/Desktop/review-study-app/out/production/classes/com/example/review_study_app/service/log/LogDirectSaveGoogleSheetsService.class]
↑     ↓
|  logGoogleSheetsRepository defined in file [/Users/daheekwak/Desktop/review-study-app/out/production/classes/com/example/review_study_app/repository/log/LogGoogleSheetsRepository.class]
↑     ↓
|  googleSheetsClient defined in file [/Users/daheekwak/Desktop/review-study-app/out/production/classes/com/example/review_study_app/infrastructure/googlesheets/GoogleSheetsClient.class]
└─────┘

Action:

Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.

Process finished with exit code 1

해결 과정

1. 에러 메시지 해석

2. 어떤 순환 참조가 형성되었는지 파악

스크린샷 2024-08-23 오전 1 30 01

3. 왜 순환 참조가 일어나는지 파악

4. 어떻게 순환 참조 문제를 해결할 것인지

스크린샷 2024-08-23 오전 1 29 11
@Configuration
public class GoogleSheetsConfig {

    @Bean
    public Sheets sheets(GoogleSheetsFactory googleSheetsFactory) {
        return googleSheetsFactory.createSheets();
    }
}

결과 (관련 commit)

daadaadaah commented 3 months ago

2번째 순환 참조 문제 관련 코드

@Autowired
public DiscordNotificationService(
    RestTemplateHttpClient restTemplateHttpClient
) {
    this.restTemplateHttpClient = restTemplateHttpClient;
}
@Autowired
public RestTemplateHttpClient(
   RestTemplate restTemplate
) {
     this.restTemplate = restTemplate;
}

@Configuration
public class AppConfig {

    @Autowired
    public AppConfig(
        GitHubApiLoggingInterceptor gitHubApiLoggingInterceptor
    ) {
        this.gitHubApiLoggingInterceptor = gitHubApiLoggingInterceptor;
    }

    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        // ... 생략
        return restTemplate;
    }
}
@Autowired
public GitHubApiLoggingInterceptor(
    LogService logService
) {
    this.logService = logService;
}
@Autowired
public LogDirectSaveGoogleSheetsService(
    NotificationService notificationService
) {
    this.notificationService = notificationService;
}
@Autowired
public DiscordNotificationService(
   RestTemplateHttpClient restTemplateHttpClient
) {
    this.restTemplateHttpClient = restTemplateHttpClient;
}