🚨ERROR🚨

Failed to configure a DataSource - 데이터소스 구성 실패

코딩베이비 2023. 5. 2. 19:16

SpringBoot 3가 릴리즈 되면서 javax.* 패키지들이 jakarta.*로 바뀌었다는 내용을 봤다.

 

괜히 직접 확인해보고 싶었다.

 

아무 생각 없이 build.gradle에 spring-boot-starter-data-jpa를 추가하고 바뀐 패키지 명을 확인했다.

 

잘 바뀌어있었다.

 

아무 생각 없이 어플리케이션을 실행했다.

 

에러발생!

WARN 10524 --- [           main]
ConfigServletWebServerApplicationContext : Exception encountered during context initialization
- cancelling refresh attempt: 
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource
[org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]:
Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0:
Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]:
Failed to instantiate [com.zaxxer.hikari.HikariDataSource]:
Factory method 'dataSource' threw exception with message:
Failed to determine a suitable driver class

...

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class

 

원인

Spring Boot가 dependencies를 기준으로 빈들을 자동구성하는데, JPA DataSource를 자동으로 구성하려고 하니 dataSource에 대한 정보가 없어서 에러가 난 것이다.

 

 

 

해결

1. Define dataSource 

spring:
  h2:
    console:
      enabled: true
      path: /h2
  datasource:
    url: jdbc:h2:mem:test

 

2. Exclude DataSourceAutoConfiguration 

// exclude 속성 활용하거나
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class HelloSpringApplication {

	public static void main(String[] args) {
		SpringApplication.run(HelloSpringApplication.class, args);
	}
}

// yml파일에 설정 추가하기
spring:
  autoconfigure:
    exclude:
      - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

 

참고

https://www.baeldung.com/spring-boot-failed-to-configure-data-source