Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.context.annotation.Primary;
  4. import org.springframework.context.annotation.PropertySource;
  5. import org.springframework.core.env.Environment;
  6. import org.springframework.orm.jpa.JpaTransactionManager;
  7. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  8. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  9. import org.springframework.transaction.PlatformTransactionManager;
  10. import org.springframework.transaction.annotation.TransactionManagementConfigurer;
  11.  
  12. import javax.persistence.EntityManagerFactory;
  13. import javax.sql.DataSource;
  14. import java.util.Properties;
  15.  
  16. @Configuration
  17. public class PersistanceContext implements TransactionManagementConfigurer {
  18.  
  19. @Bean
  20. @Primary
  21. public DataSource configureDataSource(Environment env) {
  22. org.apache.commons.dbcp.BasicDataSource source =new org.apache.commons.dbcp.BasicDataSource();
  23. source.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
  24. source.setPassword(env.getProperty("spring.datasource.password"));
  25. source.setUsername(env.getProperty("spring.datasource.username"));
  26. source.setUrl(env.getProperty("spring.datasource.url"));
  27. return source;
  28. }
  29.  
  30. @Bean
  31. LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,
  32. Environment env) {
  33. LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
  34. bean.setDataSource(dataSource);
  35.  
  36. HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
  37. adapter.setDatabasePlatform(env.getProperty("spring.jpa.database-platform"));
  38. adapter.setShowSql(env.getProperty("spring.jpa.properties.hibernate.show_sql", Boolean.class));
  39.  
  40. bean.setJpaVendorAdapter(adapter);
  41.  
  42. Properties properties = new Properties();
  43.  
  44. properties.put("hibernate.show_sql", env.getProperty("spring.jpa.properties.hibernate.show_sql", Boolean.class));
  45. properties.put("hibernate.cache.provider_class", env.getProperty("spring.jpa.properties.hibernate.cache.provider_class"));
  46. properties.put("hibernate.cache.use_second_level_cache", env.getProperty("spring.jpa.properties.hibernate.cache.use_second_level_cache", Boolean.class));
  47.  
  48. bean.setJpaProperties(properties);
  49. bean.setPackagesToScan("GrowUp.Model");
  50. return bean;
  51. }
  52.  
  53. @Bean
  54. JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory, Environment env)
  55. {
  56. JpaTransactionManager manager = new JpaTransactionManager();
  57. manager.setEntityManagerFactory(entityManagerFactory);
  58.  
  59. return manager;
  60. }
  61.  
  62. @Bean
  63. public PlatformTransactionManager annotationDrivenTransactionManager(JpaTransactionManager transactionManager) {
  64. return transactionManager;
  65. }
  66.  
  67. @Bean
  68. public PlatformTransactionManager annotationDrivenTransactionManager() {
  69. return new JpaTransactionManager();
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement