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

┌─────┐
|  authConfig defined in URL [jar:file:/Users/yukong/repository/side-project/go-higher/in-adapter-api/build/libs/in-adapter-api-0.0.1-SNAPSHOT-plain.jar!/gohigher/config/AuthConfig.class]
↑     ↓
|  querydslPredicateArgumentResolver defined in class path resource [org/springframework/data/web/config/QuerydslWebConfiguration.class]
↑     ↓
|  org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration
└─────┘

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.

문제 배경

문제 분석

해결

변경 전 코드

package gohigher.config;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class AuthConfig implements WebMvcConfigurer {

	private static final String ALLOWED_METHOD_NAMES = "GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,PATCH";
	private static final String SPLIT_REGEX = ",";

	private final List<HandlerMethodArgumentResolver> resolvers;
	private final String allowedOrigin;

	public AuthConfig(List<HandlerMethodArgumentResolver> resolvers,
		@Value("${cors-config.allowed-origin}") String allowedOrigin) {
		this.resolvers = resolvers;
		this.allowedOrigin = allowedOrigin;
	}

	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
			.allowedOrigins(allowedOrigin)
			.allowedMethods(ALLOWED_METHOD_NAMES.split(SPLIT_REGEX))
			.allowCredentials(true)
			.exposedHeaders(HttpHeaders.LOCATION);
	}

	@Override
	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
	 	resolvers.addAll(this.resolvers);
	}
}

변경 후 코드

package gohigher.config;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import gohigher.support.auth.LoginArgumentResolver;

@Configuration
public class AuthConfig implements WebMvcConfigurer {

	private static final String ALLOWED_METHOD_NAMES = "GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,PATCH";
	private static final String SPLIT_REGEX = ",";

	@Autowired
	private LoginArgumentResolver loginArgumentResolver;

	@Value("${cors-config.allowed-origin}")
	private String allowedOrigin;

	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
			.allowedOrigins(allowedOrigin)
			.allowedMethods(ALLOWED_METHOD_NAMES.split(SPLIT_REGEX))
			.allowCredentials(true)
			.exposedHeaders(HttpHeaders.LOCATION);
	}

	@Override
	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
		resolvers.add(loginArgumentResolver);
	}
}