package io.trygvis.test.spring; import com.jolbox.bonecp.BoneCPDataSource; import io.trygvis.test.DbUtil; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.support.TransactionTemplate; import javax.sql.DataSource; import java.sql.SQLException; @Configuration @ComponentScan(basePackages = "io.trygvis") @EnableTransactionManagement public class TestConfig { @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws Exception { return new PropertySourcesPlaceholderConfigurer() {{ setProperties(System.getProperties()); setLocalOverride(true); }}; } @Bean public JdbcTemplate jdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean public DataSource dataSource(@Value("${database.url}") String jdbcUrl, @Value("${database.username}") String username, @Value("${database.password}") String password) throws SQLException { BoneCPDataSource ds = DbUtil.createDataSource(jdbcUrl, username, password); return new TransactionAwareDataSourceProxy(new LazyConnectionDataSourceProxy(ds)); } @Bean public PlatformTransactionManager transactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean public TransactionTemplate transactionTemplate(PlatformTransactionManager platformTransactionManager) { return new TransactionTemplate(platformTransactionManager); } }