본문 바로가기
공부방/Java

Autowired 어노테이션

by SmartCow 2022. 11. 15.

이거때문에... 며칠을 삽질했던것인가...

 

이제 만들기시작한 Controller에 Service를 하나만 입력하고서

무의식적으로 Autowired 어노테이션을 넣어놨더니

 

오류 일부 발췌

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'xxxController': Unsatisfied dependency expressed through field 'xxxService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'xxxxService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Error creating bean with name 'xxxController': Unsatisfied dependency expressed through field 'xxxService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'xxxxService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

 

 

@Autowired 어노테이션 

생성자, 필드(프로퍼티), 설정메서드(set메서드)에 지정 가능

타입을 이용해서 자동적으로 프로퍼티 값을 설정하기 때문에 해당 타입의 빈 객체가 존재하지 않거나 빈 객체가 두 개 이상 존재할 경우 예외 발생.

 

@Autowired 어노테이션 예외 발생(여기서 에러 발생한 거였다...)

해당 타입의 빈 객체가 존재하지 않거나 빈 객체가 두 개 이상 존재할 경우 예외가 발생한다.

 

1. 해당 타입의 빈 객체가 존재하지 않는 경우 예외 처리

@Autowired(required=false)로 지정: 기본값은 @Autowired(required=true)

false로 하면 빈 객체가 존재하지 않더라도 예외처리를 발생시키지 않는다.

(이걸 하던가 아니면 Service가 늘어나기전까지는 어노테이션 설정을 하지 않았어야 했다.)

 

2. 동일한 타입의 빈 객체가 두 개 이상 존재하는 경우 예외 처리

@Qualifier("설정파일에서 지정한 값")으로 지정

 

P.S)

현업으로 일한지 오래되었지만

용어 등등은 아직도 헷갈리고 맞춰서 못하는 부분도 있어서 나도 배워가면서 하는중인데

이런식으로 뒤통수 맞을땐 어우....

1개만 연결해서 오류였다니... 찾아볼땐 무슨 web.xml에 중복 입력된게 있느니 어쩌느니

그런글들도 많은데.. 물론 그거로 해결된 분도 있겠지..

구글검색은 역시 필수야... 으허허

'공부방 > Java' 카테고리의 다른 글

XML 쿼리에서 비교연산자 에러 해결( <![CDATA[ > ]]> )  (0) 2024.05.22
web.xml 생성  (0) 2022.11.05
Maven없이 JAVA 프로젝트 만들기  (0) 2022.11.05
Tomcat server.xml 분석  (0) 2022.03.14
favicon.ico 에러  (0) 2021.03.01