Springboot

Springboot - Field required a bean of type '...' that could not be found (Bean 주입 실패)

수아파파's 2025. 3. 18. 20:04
반응형

1️⃣ 1. Field required a bean of type '...' that could not be found (Bean 주입 실패 오류)

🔹 Spring Boot 개발 시 자주 발생하는 오류 유형

  • 환경 설정 오류 → application.properties 설정 실수, 포트 충돌
  • 의존성 문제 → Gradle/Maven 라이브러리 충돌, 버전 불일치
  • 데이터베이스 관련 오류 → JPA 설정 오류, DataIntegrityViolationException
  • Spring Security 인증 오류 → JWT, OAuth 설정 문제
  • REST API 호출 및 JSON 직렬화 오류

Spring Boot 개발자들이 가장 많이 검색하는 오류를 정리하고 해결 방법을 제공!


📌 1. Field required a bean of type '...' that could not be found (Bean 주입 실패 오류)

📌 오류 메시지

Field userService in com.example.demo.controller.UserController required a bean of type 'com.example.demo.service.UserService' that could not be found.

 

📌 원인

  • Spring이 해당 Bean을 찾을 수 없음
  • @Service, @Component 또는 @Repository 어노테이션 누락

📌 해결 방법
해당 클래스에 @Service 추가

@Service
public class UserService {
}
@RequiredArgsConstructor
@RestComponent
public class UserController{
   private final UserService userService;
}

 

@RequiredArgsConstructor  lombok을 사용하여, UserService의 생성자 자동 생성하여 Bean 주입.

 

컴포넌트 스캔 범위 확인 (@ComponentScan)

@SpringBootApplication(scanBasePackages = "com.example.demo")

 

반응형