문제상황:
Spring Boot를 사용하여 웹 서버를 개발하고, 다음과 같은 코드를 작성하여 데이터베이스에 접근하려고 했습니다.
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
그러나 다음과 같은 에러로그가 발생했습니다.
***************************
APPLICATION FAILED TO START
***************************
Description:
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
해결방법:
다음과 같이 application.properties 파일에 데이터베이스 연결 정보를 추가하여 에러를 해결했습니다.
spring.datasource.url=jdbc:mysql://localhost:3306/myDatabase
spring.datasource.username=myUsername
spring.datasource.password=myPassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
원인분석:
이 에러는 데이터베이스 연결 정보가 제대로 설정되지 않아 발생합니다. Spring Boot는 기본적으로 application.properties 파일에 정의된 데이터베이스 연결 정보를 사용하여 DataSource를 설정합니다. 기존 코드에서는 해당 정보가 설정되지 않아 에러가 발생한 것입니다. 해결 방법으로 수정된 코드에서는 데이터베이스 연결 정보를 application.properties 파일에 추가하여 DataSource를 제대로 설정할 수 있도록 했습니다.
참고링크:
Spring Boot: Configuring a DataSource
[Spring | Home
Cloud Your code, any cloud—we’ve got you covered. Connect and scale your services, whatever your platform.
spring.io](https://spring.io/guides/gs/relational-data-access/)
Spring Boot: Common Application Properties
[Common Application Properties
docs.spring.io](https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html)