package org.ros.chatto.config; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; import lombok.Getter; import lombok.extern.slf4j.Slf4j; @Configuration @Component // @ConfigurationProperties(prefix = "chatto.datasource") @Getter // @Setter @Slf4j public class DataSourceConfig { // jdbc:mysql://localhost:3306/chatto_db?useSSL=false private final String DATASOURCE_URL = "jdbc:mysql://localhost:3306/%s?useSSL=false"; private final String username; private final String password; private final String databaseName; public DataSourceConfig(@Value("${chatto.datasource.username}") String username, @Value("${chatto.datasource.password}") String password, @Value("${chatto.datasource.database-name}") String databaseName) { this.username = username; this.password = password; this.databaseName = databaseName; } @Bean public DataSource getDataSource() { final DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create(); dataSourceBuilder.url(String.format(DATASOURCE_URL, databaseName)); dataSourceBuilder.username(username); dataSourceBuilder.password(password); return dataSourceBuilder.build(); } }