개발/Spring Boot

[TroubleShooting] NoSuchBeanDefinitionException 해결법

nandy 2021. 3. 1. 04:06

Spring Boot 기반 어플리케이션 개발 과정에서, Build 시 아래와 같은 메시지가 발생할 수 있습니다.

 

Error Message

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authenticationServiceImpl': 
Unsatisfied dependency expressed through field 'keyRepository'; nested exception is org.springframework.beans.factoSuchBeanDefinitionException: No qualifying bean of type '***.persistence.dao.VerificationKeyRepository' available: 
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

예를 들어, 위와 같은 메시지는 Main Application 에서 VerificationRepository Component 를 Scan 하지 못하여 NoSuchBeanDefinitionException 에러가 발생하였고, 이는 아래와 같은 방법으로 해결 가능합니다.

 

@SpringBootApplication 코드 아래에 @ComponentScan 을 추가하여, basePackages 에 VerificationRepository 명에 해당되는 package 명을 넣으면 해결 가능합니다.

 

Example Code(How to solve)

@SpringBootApplication
@ComponentScan(basePackages = {
    "***.persistence.dao"
})
public class EmailAuthApplication {

    public static void main(String[] args) {
        SpringApplication.run(EmailAuthApplication.class, args);
    }

}

Referenced From (StackOverflow)