오류 경험

required a single bean, but 2 were found: 오류

개발 초보 2023. 3. 28. 12:02
public class FieldLogTrace implements LogTrace

이처럼 인터페이스를 구현한 클래스의 경우,

package hello.advanced;

import hello.advanced.trace.logtrace.FieldLogTrace;
import hello.advanced.trace.logtrace.LogTrace;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LogTraceConfig {

    @Bean
    public LogTrace logTrace() {
        return new FieldLogTrace();
    }
}

이렇게 빈 설정을 해도

required a single bean, but 2 were found: 오류가 날 수 있다.

 

그럴 때는 구현한 클래스에 @Primary를 달아줘라.

 

@Slf4j
@Component
@Primary
public class FieldLogTrace implements LogTrace

내가 당했다.

이걸 보는 분은 오류 처리가 빠르게 되었으면 좋겠다.