required a single bean, but more than one were found.

서민정·2023년 11월 10일
Parameter 0 of method SomethingClass in SomethingClass required a single bean, but 14 were found:
	- 어짜고 저짜고
... 이하동문

왜 이런 문제가 발생했냐!

somethingClass는 library로 제공되던 클래스인데, 빈으로 등록되기 위해서 다음과 같은 조건이 있었다.


@ConditionalOnMissingBean(name = ["somethingClassPrepared"])
@ConfigurationProperties(prefix = "something.some.some", ignoreUnknownFields = true)
@Bean
fun somethingClassPrepared(): BaseGrpcProperties {
    return BaseGrpcProperties()
}

즉, somethingClass가 빈을 만들기 위한 조건이 @ConditionalOnMissingBean(name = ["somethingClassPrepared"]) 즉, somethingClassPrepared 라는 이름을 갖는 bean이 없는 경우인 것이다.

하지만 프로젝트에서 정의되어있던 Configuration 파일들을 뒤져보니 이미 같은 이름으로 정의된 빈이 있었다.

    @Bean
    @ConfigurationProperties("something.some.some")
    fun somethingClassPrepared(): BaseGrpcProperties {
        return BaseGrpcProperties()
    }

즉!!!!!
프로젝트에 @Configuration 애노테이션을 활용하여 정의된 빈들이 먼저 뜨고, 그 다음으로 library에 추가된 빈들이 등록되나보다. 그래서 이미 기정의된 somethingClassPrepared가 생겨났기 때문에, library에 필요한 빈들이 뜨지 못했고, 그래서 후보가 많이 생겼다.

빈 이름을 정의할 땐 주의하자!

profile
Server Engineer

0개의 댓글