[BAEL-12800] - Added MultipleDataSourceConfiguration and added autowiring of DataSources in Product and User Config classes
This commit is contained in:
parent
7fa1c651e5
commit
8e5d85cd85
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,23 +1,23 @@
|
||||||
package com.baeldung.multipledb;
|
package com.baeldung.multipledb;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Profile;
|
import org.springframework.context.annotation.Profile;
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
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.JpaTransactionManager;
|
||||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||||
import org.springframework.transaction.PlatformTransactionManager;
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@PropertySource({"classpath:persistence-multiple-db.properties"})
|
@PropertySource({"classpath:persistence-multiple-db.properties"})
|
||||||
@EnableJpaRepositories(basePackages = "com.baeldung.multipledb.dao.product", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
|
@EnableJpaRepositories(basePackages = "com.baeldung.multipledb.dao.product", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
|
||||||
|
@ -26,6 +26,10 @@ public class PersistenceProductConfiguration {
|
||||||
@Autowired
|
@Autowired
|
||||||
private Environment env;
|
private Environment env;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("productDataSource")
|
||||||
|
private DataSource productDataSource;
|
||||||
|
|
||||||
public PersistenceProductConfiguration() {
|
public PersistenceProductConfiguration() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
@ -35,7 +39,7 @@ public class PersistenceProductConfiguration {
|
||||||
@Bean
|
@Bean
|
||||||
public LocalContainerEntityManagerFactoryBean productEntityManager() {
|
public LocalContainerEntityManagerFactoryBean productEntityManager() {
|
||||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||||
em.setDataSource(productDataSource());
|
em.setDataSource(productDataSource);
|
||||||
em.setPackagesToScan("com.baeldung.multipledb.model.product");
|
em.setPackagesToScan("com.baeldung.multipledb.model.product");
|
||||||
|
|
||||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||||
|
@ -49,9 +53,14 @@ public class PersistenceProductConfiguration {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConfigurationProperties(prefix="spring.product")
|
|
||||||
public DataSource productDataSource() {
|
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
|
@Bean
|
||||||
|
|
|
@ -1,24 +1,20 @@
|
||||||
package com.baeldung.multipledb;
|
package com.baeldung.multipledb;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
import org.springframework.context.annotation.*;
|
||||||
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.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
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.JpaTransactionManager;
|
||||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||||
import org.springframework.transaction.PlatformTransactionManager;
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@PropertySource({"classpath:persistence-multiple-db.properties"})
|
@PropertySource({"classpath:persistence-multiple-db.properties"})
|
||||||
@EnableJpaRepositories(basePackages = "com.baeldung.multipledb.dao.user", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager")
|
@EnableJpaRepositories(basePackages = "com.baeldung.multipledb.dao.user", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager")
|
||||||
|
@ -27,6 +23,10 @@ public class PersistenceUserConfiguration {
|
||||||
@Autowired
|
@Autowired
|
||||||
private Environment env;
|
private Environment env;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("userDataSource")
|
||||||
|
private DataSource userDataSource;
|
||||||
|
|
||||||
public PersistenceUserConfiguration() {
|
public PersistenceUserConfiguration() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ public class PersistenceUserConfiguration {
|
||||||
public LocalContainerEntityManagerFactoryBean userEntityManager() {
|
public LocalContainerEntityManagerFactoryBean userEntityManager() {
|
||||||
System.out.println("loading config");
|
System.out.println("loading config");
|
||||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||||
em.setDataSource(userDataSource());
|
em.setDataSource(userDataSource);
|
||||||
em.setPackagesToScan("com.baeldung.multipledb.model.user");
|
em.setPackagesToScan("com.baeldung.multipledb.model.user");
|
||||||
|
|
||||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||||
|
@ -51,11 +51,15 @@ public class PersistenceUserConfiguration {
|
||||||
return em;
|
return em;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Primary
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConfigurationProperties(prefix="spring.user")
|
|
||||||
public DataSource userDataSource() {
|
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
|
@Primary
|
||||||
|
|
|
@ -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
|
|
@ -1,14 +1,9 @@
|
||||||
# user data source
|
# jdbc.X
|
||||||
spring.user.driverClassName=org.h2.Driver
|
jdbc.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
|
user.jdbc.url=jdbc:h2:mem:spring_jpa_user;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS USERS
|
||||||
spring.user.username=sa
|
product.jdbc.url=jdbc:h2:mem:spring_jpa_product;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS PRODUCTS
|
||||||
spring.user.username=sa
|
jdbc.user=sa
|
||||||
|
jdbc.pass=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.X
|
||||||
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||||
|
|
Loading…
Reference in New Issue