[Spring Boot][오류해결] Parameter 0 of constructor in ~~ required a single bean, but 2 were found

2022. 2. 13. 13:00·JAVA/오류해결

 

Description:

Parameter 0 of constructor in com.bigbell.springboot.cruddemo.service.EmployeeServiceImpl 
required a single bean, but 2 were found:
- employeeDAOHibernateImpl: defined in file [D:\bigbell\Spring-Boot\22-jpa-crud-
demo\target\classes\com\bigbell\springboot\cruddemo\dao\EmployeeDAOHibernateImpl.class]
- employeeDAOJpaImpl: defined in file [D:\bigbell\Spring-Boot\22-jpa-crud-
demo\target\classes\com\bigbell\springboot\cruddemo\dao\EmployeeDAOJpaImpl.class]

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using 
@Qualifier to identify the bean that should be consumed

 

스프링부트를 사용할 때 발생하는 에러이다.

APPLICATION FAILED TO START 에러가 발생하면 위와 같이 자세한 설명이 나오는데

문제는 다음과 같다.

특정 인터페이스를 상속한 클래스 파일이 여러 개일 때

의존성 주입을 위해 @Primary, @Qualifier 어노테이션을 사용해야하는데

@Autowired를 적었기 때문에 여러 개의 클래스가 충돌하여 에러가 발생하는 것이다.

 

 

Action에 나온 것을 그대로 적용해주면 해결된다.

대충 해석해보자면, @Primary 어노테이션을 통해 우선순위를 정해주거나

@Qualifier 어노테이션으로 빈 하나를 설정해주라는 거 같다.

 

 

 

예시1


문제상황  - MyInterface를 상속한 클래스가 여러 개일때, 멤버변수에 의존성 주입

@Autowired
private MyInterface myInterface;

 

해결

@Qualifier("클래스명")
private MyInterface myInterface;

 

 

 

예시2


문제상황 - MyInterface를 상속한 클래스가 여러 개일때, 생성자 인자에 의존성 주입

private MyInterface myInterface;

@Autowired
public 생성자(MyInterface myInterface) {
    this.myInterface = myInterface;
}

 

해결

private MyInterface myInterface;

@Autowired
public 생성자(@Qualifier("클래스명") MyInterface myInterface) {
    this.myInterface = myInterface;
}

 

 

 

'JAVA/오류해결' 카테고리의 다른 글
  • [Spring Boot][오류해결] Executing an update/delete query
  • [Spring Boot][오류해결] Parameter 0 of constructor in ~~ required a bean of type ~~ that could not be found.
  • [Spring Boot][오류해결] org.springframework.beans.factory.UnsatisfiedDependencyException
  • [오류해결] Several ports (8005, 8080) required by Tomcat v9.0 Server at localhost are already in use
gakko
gakko
좌충우돌 개발기
  • gakko
    MYVELOP 마이벨롭
    gakko
  • 전체
    오늘
    어제
    • 분류 전체보기 (203) N
      • Spring (23) N
        • Spring (10)
        • Spring Boot (7)
        • Spring Security (1)
        • Hibernate (4) N
      • Test (3)
      • 끄적끄적 (6)
      • 활동 (35)
        • 부스트캠프 (23)
        • 동아리 (3)
        • 컨퍼런스 (3)
        • 글또 (5)
        • 오픈소스 컨트리뷰션 (1)
      • 디자인패턴 (0)
      • Git & GitHub (22)
        • Git (13)
        • Github Actions (1)
        • 오류해결 (5)
        • 기타(마크다운 등) (3)
      • 리눅스 (6)
        • 기초 (6)
        • 리눅스 서버 구축하기 (0)
      • Infra (2)
        • Docker (1)
        • Elastic Search (0)
        • Jenkins (1)
        • AWS (1)
      • MySQL (7)
        • 기초 (6)
        • Real MySQL (1)
      • 후기 (3)
        • Udemy 리뷰 (3)
      • CS (26)
        • 웹 기본지식 (0)
        • 자료구조 (13)
        • 운영체제 OS (12)
        • 데이터베이스 (1)
        • 시스템 프로그래밍 (0)
        • 기타 (0)
      • Tools (1)
        • 이클립스 (1)
        • IntelliJ (0)
      • 프로젝트 (1)
        • 모여모여(부스트캠프) (1)
      • JAVA (32)
        • Maven (6)
        • 오류해결 (11)
        • 자바 클래스&메소드 (1)
        • JSP & Servlet (12)
      • Javascript (5)
        • 기초 (3)
        • React (2)
      • Python (28)
        • 파이썬 함수 (9)
        • 알고리즘 문제풀이 (16)
        • 데이터 사이언스 (2)
        • 웹 크롤링 (1)
      • 단순정보전달글 저장소 (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • 우진님
  • 공지사항

  • 인기 글

  • 태그

    스프링
    웹개발
    파이썬
    java
    부스트캠프 멤버십
    운영체제
    오류해결
    Python
    GitHub
    스프링부트
    MySQL
    알고리즘
    자바
    os
    jsp
    Git
    Spring
    부스트캠프
    자바스크립트
    부스트캠프 7기
  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.0
gakko
[Spring Boot][오류해결] Parameter 0 of constructor in ~~ required a single bean, but 2 were found
상단으로

티스토리툴바