[Spring] org.springframework.beans.factory.UnsatisfiedDependencyException

Dana's Log·2022년 5월 20일
0

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userMapper' defined in file [/Users/dana/eclipse-workspace/demo/bin/main/com/finger/user/mapper/UserMapper.class]: Cannot resolve reference to bean 'sqlSessionTemplate' while setting bean property 'sqlSessionTemplate'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionTemplate' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException:
...

아래는 여러 해결방법 참고..
나같은 경우 vo를 추가해 놓고 mybatis-config 에서 설정을 안 해줬다..



Error creating bean with name "XXXController"

만약 컨트롤러(Controller) 에서 빈생성 에러가 난다면, 그것은 servlet(서블릿)에서 에러의 원인을 의심해보아야 한다.

스프링의 전체적인 데이터 순환? 을 간단히 적어서 생각해보면

사용자가 어떤 요청을 하면 그것을 가장 먼저 Servlet이 받아서 Controller에 Mapping을 확인 한 뒤, 해당 Mapping의 코드로 전달되고 이후에 Service단을 통해 DAO를 거쳐가는 것이 먼저 학습되어 있어야 한다.

주로 빈 생성 에러는 서버를 새로 시작할 때 에러가 발생 할텐데 서버가 시작하고 / 혹은 index.do 와 같은 메인 페이지에 요청을 보낼 것이다. 이 때 Servlet이 요청을 받고 Controller 에 있는 Mapping을 확인해야 하는데 Controller 자체를 찾을 수 없다면? 코드의 문제가 아니라 이전의 Servlet의 properties.. 즉 설정값 세팅 에서 에러가 발생했을 것이다.

해결 방법(3가지 확인)

고로 Controller 빈 생성 에러가 발생 했다면 servlet-context.xml 같은 servlet을 확인해서

<context:component-scan base-package="패키지명" />에 현재 작업중인 프로젝트의 패키지명.. 즉 컨트롤러가 포함된 패키지의 위치를 정확하게 기재 하시길.. (그냥 컨트롤러에 패키지를 그대로 복사해서 넣으시는 걸 추천)

그리고 @Controller 어노테이션이 없어서 일수도 있다 -> 이건? 당연하다. Controller를 찾아서 URL의 Mapping을 찾아야 하는데.. 컨트롤러 자체를 찾지 못했을테니

"그래도 안되는데??"

1. Unsatisfied dependency expressed through field 'XXXService';

만약 servlet-context.xml에 (이하 servlet)에서 패키지를 입력하고도.. 또 빈 생성 에러가 발생할 수 있다

근데 끝까지 읽어보면 뒤에 Service단에서 에러가 발생한다. 이는.. Service단에 어노테이션이 없어서 일것이다

@Service 혹은 @Component 같은 어노테이션을 추가한다. 기존 프로젝트가 있다면 다른 서비스단을 참조하면 된다.

"그래도 안되는데??"

2. Unsatisfied dependency expressed through field 'XXXServiceImpl';

그렇다 만약이지만 또 안될 수도 있다. -_-;; 이번엔 과연 뭐 때문일까?
처음에는 인터페이스인 Service 였는데.. 이번엔 인터페이스를 구현하고 있는 ServiceImpl에서 빈 생성의 이유가 나온다
마찬가지로 ServiceImpl에 가서 @Repository 어노테이션을 추가한다

스프링의 어노 테이션을 조금 더 구체적으로 익혀두면 디버그시 어떤 문제가 있는지 추론하여 디버그를 할수 있다!

3. 작성한 sql이 담긴 mybatis (xml파일) 확인 해볼 것 (작성한 SQL 오타 혹은 에러)

트랙백에서 보니 SQL문을 담고있는 mapper xml에서 쿼리에 오타가 발생했거나 에러가 발생하면 빈 에러가 발생하기도 한다고 한다.
SQL의 경우 쿼리를 날려보고 오류 여부를 확인하여 반영하도록 하자.
내 포스팅의 경우 주로 Servies나 DAO를 추가했을때 빈번하게 나는 에러이며 프로젝트 초기에 세팅시 발생하는 경우 위주로 작성 하였다.

출처: https://liante0904.tistory.com/113 [Liante의 이야기]

profile
다나로그

0개의 댓글