Guest User

Untitled

a guest
Nov 27th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. spring.datasource.url=jdbc:h2:mem
  2. spring.datasource.username=
  3. spring.datasource.password=
  4. spring.datasource.driver-class-name=org.h2.Driver
  5.  
  6. another.datasource.url=jdbc:mysql://localhost:3306/test
  7. another.datasource.username=root
  8. another.datasource.password=
  9. another.datasource.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
  10. another.datasource.driverClassName=com.mysql.jdbc.Driver
  11.  
  12. @Configuration
  13. @EnableTransactionManagement
  14. @EnableJpaRepositories(
  15. entityManagerFactoryRef = "anotherEntityManagerFactory",
  16. transactionManagerRef = "anotherTransactionManager",
  17. basePackages = {"test.repository.mysql"})
  18. public class RepositoryConfig {
  19.  
  20. @Autowired
  21. JpaVendorAdapter jpaVendorAdapter;
  22.  
  23. @Value("${another.datasource.url}")
  24. private String databaseUrl;
  25.  
  26. @Value("${another.datasource.username}")
  27. private String username;
  28.  
  29. @Value("${another.datasource.password}")
  30. private String password;
  31.  
  32. @Value("${another.datasource.driverClassName}")
  33. private String driverClassName;
  34.  
  35. public DataSource dataSource() {
  36. DriverManagerDataSource dataSource = new DriverManagerDataSource(databaseUrl, username, password);
  37. dataSource.setDriverClassName(driverClassName);
  38. return dataSource;
  39. }
  40.  
  41. @Bean(name = "anotherEntityManager")
  42. public EntityManager entityManager() {
  43. return entityManagerFactory().createEntityManager();
  44. }
  45.  
  46. @Bean(name = "anotherEntityManagerFactory")
  47. public EntityManagerFactory entityManagerFactory() {
  48. Properties properties = new Properties();
  49. LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
  50. emf.setDataSource(dataSource());
  51. emf.setJpaVendorAdapter(jpaVendorAdapter);
  52. emf.setPackagesToScan("test.entity.mysql"); // <- package for entities
  53. emf.setPersistenceUnitName("anotherEntity");
  54. emf.setJpaProperties(properties);
  55. emf.afterPropertiesSet();
  56. return emf.getObject();
  57. }
  58.  
  59. @Bean(name = "anotherTransactionManager")
  60. public PlatformTransactionManager transactionManager() {
  61. return new JpaTransactionManager(entityManagerFactory());
  62. }
  63. }
  64.  
  65. @Bean
  66. public LocalContainerEntityManagerFactoryBean customerEntityManagerFactory(
  67. EntityManagerFactoryBuilder builder) {
  68. return builder
  69. .dataSource(customerDataSource())
  70. .packages(Customer.class)
  71. .persistenceUnit("customers")
  72. .build();
  73. }
  74.  
  75. @Bean
  76. public LocalContainerEntityManagerFactoryBean orderEntityManagerFactory(
  77. EntityManagerFactoryBuilder builder) {
  78. return builder
  79. .dataSource(orderDataSource())
  80. .packages(Order.class)
  81. .persistenceUnit("orders")
  82. .build();
  83. }
  84.  
  85. @Configuration
  86. @EnableJpaRepositories(basePackageClasses = Customer.class,
  87. entityManagerFactoryRef = "customerEntityManagerFactory")
  88. public class CustomerConfiguration {
  89. ...
  90. }
  91.  
  92. @Configuration
  93. @EnableJpaRepositories(basePackageClasses = Order.class,
  94. entityManagerFactoryRef = "orderEntityManagerFactory")
  95. public class OrderConfiguration {
  96. ...
  97. }
  98.  
  99. @Bean(name = "mysql")
  100. @Primary
  101. @ConfigurationProperties(prefix="datasource.mysql")
  102. public DataSourceProperties fooDataSourceProperties() {
  103. return DataSourceBuilder
  104. .create()
  105. .build();
  106. }
  107.  
  108. @Bean(name = "postgres")
  109. public DataSource fooDataSource() {
  110. return fooDataSourceProperties().initializeDataSourceBuilder().build();
  111. }
Add Comment
Please, Sign In to add comment