Spring

[Spring] - application.properties DB접속정보로 DataSource 빈 생성이 안된이슈

김종현 2020. 12. 2. 02:16

아래사진은 이전글의 Test 했던상황인데 에러메세지가 NoSuchBeanDefinitaionException

즉, 해당 Bean을 찾지못했다는 이유이다.

말 그대로 DataSource 인터페이스의 구현체를 찾을수 없다는 내용이었다.

application.properties에 DB접속정보도 잘 넣어주었는데 왜 빈이 생성이 안되는지 한참을 찾아보니 이런글을 발견하였다.

 

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-sql

 

Spring Boot Features

Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and Servlet-based web applications. It occurs as part of closing the application context and is performed in the earliest

docs.spring.io

해당링크 11.1.2 를 확인해보면 다음과 같은 사실을 알 수 있었다.

 

Spring Boot 에서 는 기본적으로 Connection pool을 HiKariCP를 사용하게 되는데 그러기 위해선

pom.xml에 HiKariCP 의존성을 추가하고 설정(java, properties, yml)파일을 만들어서 DB접속정보

를 HiKariDataSource 설정을 잡아줘야 DataSource 빈을 생성할 수 있었다.

 

하지만 필자는 HiKariCP 의존성을 추가하지 않았으니 당연히 빈 생성도 할 수 없었던 것....

 

그런데 해당링크 마지막 문구를 보면 spring-boot-starter-jdbc 또는 spring-boot-starter-data-jpa를 pom.xml에 추가하면 자동으로 HiKariCP의 Connection pool을 얻을 수 있다고 하길레 추가해주고 테스트를 진행해보았다

 

 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

 

 

 

Test가 정상적으로 잘돌아갔다. ㅎㅎ

 

여태껏 개발을 1년 반동안 해오면서 Test 코드 작성을 거의 하지않았는데 이번에 블로그활동도 하면서 

열심히 Test 코드를 작성해보겠다.

 

(Test 코드 저거 하나 만드는데 시간을 얼마나 날린건지..ㅋㅋ)

728x90