[BAEL-12800] - Added MultipleDataSourceConfiguration and added autowiring of DataSources in Product and User Config classes

This commit is contained in:
amit2103 2019-05-02 00:35:32 +05:30
parent 7fa1c651e5
commit 8e5d85cd85
5 changed files with 94 additions and 35 deletions

View File

@ -0,0 +1,33 @@
package com.baeldung.multipledb;
import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/**
* By default, the persistence-multiple-db.properties file is read for productDataSource() and userDataSource()
* in PersistenceProductConfiguration and PersistenceUserConfiguration respectively.
* <p>
* If we need to use persistence-multiple-db-boot.properties then uncomment the below @Configuration class and comment out
* productDataSource() and userDataSource() in PersistenceProductConfiguration and PersistenceUserConfiguration.
*/
//@Configuration
@PropertySource({"classpath:persistence-multiple-db-boot.properties"})
public class MultipleDataSourceConfiguration {
@Bean
@ConfigurationProperties(prefix="spring.user")
public DataSource userDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix="spring.product")
public DataSource productDataSource() {
return DataSourceBuilder.create().build();
}
}

View File

@ -1,23 +1,23 @@
package com.baeldung.multipledb;
import java.util.HashMap;
import javax.sql.DataSource;
import com.google.common.base.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
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
@PropertySource({"classpath:persistence-multiple-db.properties"})
@EnableJpaRepositories(basePackages = "com.baeldung.multipledb.dao.product", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
@ -26,6 +26,10 @@ public class PersistenceProductConfiguration {
@Autowired
private Environment env;
@Autowired
@Qualifier("productDataSource")
private DataSource productDataSource;
public PersistenceProductConfiguration() {
super();
}
@ -35,7 +39,7 @@ public class PersistenceProductConfiguration {
@Bean
public LocalContainerEntityManagerFactoryBean productEntityManager() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(productDataSource());
em.setDataSource(productDataSource);
em.setPackagesToScan("com.baeldung.multipledb.model.product");
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
@ -49,9 +53,14 @@ public class PersistenceProductConfiguration {
}
@Bean
@ConfigurationProperties(prefix="spring.product")
public DataSource productDataSource() {
return DataSourceBuilder.create().build();
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("product.jdbc.url")));
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
return dataSource;
}
@Bean

View File

@ -1,24 +1,20 @@
package com.baeldung.multipledb;
import java.util.HashMap;
import javax.sql.DataSource;
import com.google.common.base.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
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
@PropertySource({"classpath:persistence-multiple-db.properties"})
@EnableJpaRepositories(basePackages = "com.baeldung.multipledb.dao.user", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager")
@ -27,6 +23,10 @@ public class PersistenceUserConfiguration {
@Autowired
private Environment env;
@Autowired
@Qualifier("userDataSource")
private DataSource userDataSource;
public PersistenceUserConfiguration() {
super();
}
@ -38,7 +38,7 @@ public class PersistenceUserConfiguration {
public LocalContainerEntityManagerFactoryBean userEntityManager() {
System.out.println("loading config");
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(userDataSource());
em.setDataSource(userDataSource);
em.setPackagesToScan("com.baeldung.multipledb.model.user");
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
@ -51,11 +51,15 @@ public class PersistenceUserConfiguration {
return em;
}
@Primary
@Bean
@ConfigurationProperties(prefix="spring.user")
public DataSource userDataSource() {
return DataSourceBuilder.create().build();
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("user.jdbc.url")));
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
return dataSource;
}
@Primary

View File

@ -0,0 +1,18 @@
# user data source
spring.user.driverClassName=org.h2.Driver
spring.user.jdbc-url=jdbc:h2:mem:spring_jpa_user;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS USERS
spring.user.username=sa
spring.user.username=sa
# product data source
spring.product.driverClassName=org.h2.Driver
spring.product.jdbc-url=jdbc:h2:mem:spring_jpa_product;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS PRODUCTS
spring.product.username=sa
spring.product.username=sa
# hibernate.X
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create-drop
hibernate.cache.use_second_level_cache=false
hibernate.cache.use_query_cache=false

View File

@ -1,14 +1,9 @@
# user data source
spring.user.driverClassName=org.h2.Driver
spring.user.jdbc-url=jdbc:h2:mem:spring_jpa_user;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS USERS
spring.user.username=sa
spring.user.username=sa
# product data source
spring.product.driverClassName=org.h2.Driver
spring.product.jdbc-url=jdbc:h2:mem:spring_jpa_product;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS PRODUCTS
spring.product.username=sa
spring.product.username=sa
# jdbc.X
jdbc.driverClassName=org.h2.Driver
user.jdbc.url=jdbc:h2:mem:spring_jpa_user;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS USERS
product.jdbc.url=jdbc:h2:mem:spring_jpa_product;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS PRODUCTS
jdbc.user=sa
jdbc.pass=sa
# hibernate.X
hibernate.dialect=org.hibernate.dialect.H2Dialect