[BAEL-12800] - Added auto config classes for DataSource

This commit is contained in:
amit2103 2019-05-19 15:48:35 +05:30
parent 567fcc1ba0
commit 2c0f03012a
5 changed files with 150 additions and 45 deletions

View File

@ -1,33 +0,0 @@
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

@ -0,0 +1,71 @@
package com.baeldung.multipledb;
import java.util.HashMap;
import javax.sql.DataSource;
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.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
/**
* By default, the persistence-multiple-db.properties file is read for
* non auto configuration in PersistenceProductConfiguration.
* <p>
* If we need to use persistence-multiple-db-boot.properties and auto configuration
* then uncomment the below @Configuration class and comment out PersistenceProductConfiguration.
*/
//@Configuration
@PropertySource({"classpath:persistence-multiple-db-boot.properties"})
@EnableJpaRepositories(basePackages = "com.baeldung.multipledb.dao.product", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
@Profile("!tc")
public class PersistenceProductAutoConfiguration {
@Autowired
private Environment env;
public PersistenceProductAutoConfiguration() {
super();
}
//
@Bean
public LocalContainerEntityManagerFactoryBean productEntityManager() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(productDataSource());
em.setPackagesToScan("com.baeldung.multipledb.model.product");
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
final HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Bean
@ConfigurationProperties(prefix="spring.product")
public DataSource productDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public PlatformTransactionManager productTransactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(productEntityManager().getObject());
return transactionManager;
}
}

View File

@ -2,7 +2,6 @@ package com.baeldung.multipledb;
import com.google.common.base.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
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;
@ -26,10 +25,6 @@ public class PersistenceProductConfiguration {
@Autowired
private Environment env;
@Autowired
@Qualifier("productDataSource")
private DataSource productDataSource;
public PersistenceProductConfiguration() {
super();
}
@ -39,7 +34,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();

View File

@ -0,0 +1,76 @@
package com.baeldung.multipledb;
import java.util.HashMap;
import javax.sql.DataSource;
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.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
/**
* By default, the persistence-multiple-db.properties file is read for
* non auto configuration in PersistenceUserConfiguration.
* <p>
* If we need to use persistence-multiple-db-boot.properties and auto configuration
* then uncomment the below @Configuration class and comment out PersistenceUserConfiguration.
*/
//@Configuration
@PropertySource({"classpath:persistence-multiple-db-boot.properties"})
@EnableJpaRepositories(basePackages = "com.baeldung.multipledb.dao.user", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager")
@Profile("!tc")
public class PersistenceUserAutoConfiguration {
@Autowired
private Environment env;
public PersistenceUserAutoConfiguration() {
super();
}
//
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean userEntityManager() {
System.out.println("loading config");
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(userDataSource());
em.setPackagesToScan("com.baeldung.multipledb.model.user");
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
final HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Primary
@Bean
@ConfigurationProperties(prefix="spring.user")
public DataSource userDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean
public PlatformTransactionManager userTransactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(userEntityManager().getObject());
return transactionManager;
}
}

View File

@ -2,7 +2,6 @@ package com.baeldung.multipledb;
import com.google.common.base.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
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;
@ -23,10 +22,6 @@ public class PersistenceUserConfiguration {
@Autowired
private Environment env;
@Autowired
@Qualifier("userDataSource")
private DataSource userDataSource;
public PersistenceUserConfiguration() {
super();
}
@ -38,7 +33,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,6 +46,7 @@ public class PersistenceUserConfiguration {
return em;
}
@Primary
@Bean
public DataSource userDataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();