...
이제 다음과 같이 수동으로 DataSource와 JDBC Template을 정의합니다. 단, 다음의 DataSource 설정은 꼭 필요한 경우만 하도록 하고 별도로 설정하지 않더라도 YAML 파일에 설정하고 관련 Dependency만 추가하면 spring-boot-starter-data-jpa 만 추가하면 자동으로 데이터소스가 생성됩니다.
코드 블럭 | ||||
---|---|---|---|---|
| ||||
import com.zaxxer.hikari.HikariDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; @Configuration public class DataSourceConfiguration { @Bean(name = "dataSource") @Primary @ConfigurationProperties("spring.datasource.hikari") public DataSource dataSource() { return DataSourceBuilder.create().type(HikariDataSource.class).build(); } @Bean JdbcTemplate jdbcTemplate() { return new JdbcTemplate(dataSource()); } } |
...