[SpringBoot]Failed to configure a DataSource 에러

유알·2022년 12월 5일
0

[Spring]

목록 보기
2/17

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

보통 초기에 스프링 부트 실행시키면 자주보는 에러
데이터베이스 설정을 하지 않아서 발생한다.

아래는 예시다

application.properties에 다음 추가

spring.datasource.hikari.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.hikari.jdbc-url=jdbc:mysql://localhost:3306/insight?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.hikari.username=아이디
spring.datasource.hikari.password=비번

DatabaseConfig

package ChatingWithSTOMP.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

@Configuration
@PropertySource("classpath:/application.properties")
public class DatabaseConfig {

    @Bean
    @ConfigurationProperties("spring.datasource.hikari")
    public HikariConfig hikariConfig(){
        return new HikariConfig();
    }

    @Bean
    public DataSource dataSource() throws Exception{
        DataSource dataSource = new HikariDataSource(hikariConfig());
        return dataSource;
    }

}
profile
더 좋은 구조를 고민하는 개발자 입니다

0개의 댓글