|
| 1 | +package com.sehee.weeklyjpa.config; |
| 2 | + |
| 3 | +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; |
| 4 | +import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; |
| 5 | +import org.springframework.context.annotation.Bean; |
| 6 | +import org.springframework.context.annotation.Configuration; |
| 7 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
| 8 | +import org.springframework.jdbc.datasource.DriverManagerDataSource; |
| 9 | +import org.springframework.orm.jpa.JpaTransactionManager; |
| 10 | +import org.springframework.orm.jpa.JpaVendorAdapter; |
| 11 | +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| 12 | +import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter; |
| 13 | +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
| 14 | +import org.springframework.transaction.PlatformTransactionManager; |
| 15 | + |
| 16 | +import javax.sql.DataSource; |
| 17 | +import java.util.Properties; |
| 18 | + |
| 19 | +@Configuration |
| 20 | +@EnableJpaRepositories("com.sehee.weeklyjpa.domain") |
| 21 | +public class DataSourceConfig { |
| 22 | + @Bean |
| 23 | + public DataSource dataSource(DataSourceProperties dataSourceProperties) { |
| 24 | + DriverManagerDataSource dataSource = new DriverManagerDataSource(); |
| 25 | + dataSource.setDriverClassName(dataSourceProperties.determineDriverClassName()); |
| 26 | + dataSource.setUrl(dataSourceProperties.getUrl()); |
| 27 | + dataSource.setUsername(dataSourceProperties.getUsername()); |
| 28 | + dataSource.setPassword(dataSourceProperties.getPassword()); |
| 29 | + |
| 30 | + return dataSource; |
| 31 | + } |
| 32 | + |
| 33 | + @Bean |
| 34 | + public JpaVendorAdapter jpaVendorAdapter(JpaProperties jpaProperties) { |
| 35 | + AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter(); |
| 36 | + adapter.setShowSql(jpaProperties.isShowSql()); |
| 37 | + adapter.setDatabasePlatform(jpaProperties.getDatabasePlatform()); |
| 38 | + adapter.setGenerateDdl(jpaProperties.isGenerateDdl()); |
| 39 | + |
| 40 | + return adapter; |
| 41 | + } |
| 42 | + |
| 43 | + @Bean |
| 44 | + public LocalContainerEntityManagerFactoryBean entityManagerFactory( |
| 45 | + DataSource dataSource, |
| 46 | + JpaVendorAdapter jpaVendorAdapter, |
| 47 | + JpaProperties jpaProperties |
| 48 | + ) { |
| 49 | + LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); |
| 50 | + em.setDataSource(dataSource); |
| 51 | + em.setPackagesToScan("com.sehee.weeklyjpa.domain"); |
| 52 | + em.setJpaVendorAdapter(jpaVendorAdapter); |
| 53 | + |
| 54 | + Properties properties = new Properties(); |
| 55 | + properties.putAll(jpaProperties.getProperties()); |
| 56 | + em.setJpaProperties(properties); |
| 57 | + |
| 58 | + return em; |
| 59 | + } |
| 60 | + |
| 61 | + @Bean |
| 62 | + public PlatformTransactionManager transactionManager(LocalContainerEntityManagerFactoryBean entityManagerFactory) { |
| 63 | + JpaTransactionManager transactionManager = new JpaTransactionManager(); |
| 64 | + transactionManager.setEntityManagerFactory(entityManagerFactory.getObject()); |
| 65 | + |
| 66 | + return transactionManager; |
| 67 | + } |
| 68 | +} |
0 commit comments