package app.configurations; import com.zaxxer.hikari.HikariDataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Scope; import org.springframework.core.env.Environment; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import javax.sql.DataSource; import java.util.HashMap; @Configuration @EnableJpaRepositories(basePackages = "app.entities.dummy.rw", entityManagerFactoryRef = "RwEntityManager", transactionManagerRef = "RwTransactionManager") public class DbRwConfigurarion implements DbConfiguration{ @Autowired Environment environment; private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Bean(name = "RwDataSourceProperties") @Scope(value = "singleton") @ConfigurationProperties(prefix = "backend.db.rw") public DataSourceProperties DataSourceProperties() { return new DataSourceProperties(); } @Bean(name = "RwDataSource") @Scope(value = "singleton") public DataSource DataSource() { HikariDataSource dataSource = DataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build(); dataSource.setReadOnly(false); return dataSource; } @Bean(name = "RwEntityManager") @Scope(value = "singleton") public LocalContainerEntityManagerFactoryBean EntityManager() { LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean(); entityManager.setDataSource(DataSource()); entityManager.setPackagesToScan("app.entities.dummy.rw"); HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); entityManager.setJpaVendorAdapter(vendorAdapter); HashMap properties = new HashMap<>(); properties.put("hibernate.hbm2ddl.auto", environment.getProperty("DDL_AUTO", "none")); entityManager.setJpaPropertyMap(properties); return entityManager; } @Bean(name = "RwTransactionManager") @Scope(value = "singleton") public PlatformTransactionManager TransactionManager() { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(EntityManager().getObject()); return transactionManager; } @Bean(name = "jt_rw") @Scope(value = "prototype") @Primary public JdbcTemplate getJT() { return new JdbcTemplate(DataSource()); } }