
com.github.fridujo.sample.customer.CustomerConfig Maven / Gradle / Ivy
package com.github.fridujo.sample.customer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "customerEntityManagerFactory", transactionManagerRef = "customerTransactionManager")
class CustomerConfig {
@Bean
PlatformTransactionManager customerTransactionManager(@Qualifier("customer") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
@Bean
@Qualifier("customer")
LocalContainerEntityManagerFactoryBean customerEntityManagerFactory(@Qualifier("customer") DataSource dataSource) {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
factoryBean.setPackagesToScan(CustomerConfig.class.getPackage().getName());
return factoryBean;
}
@Bean
@ConfigurationProperties("customer.datasource")
@Qualifier("customer")
DataSourceProperties customerDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@Qualifier("customer")
DataSource customerDataSource(@Qualifier("customer") DataSourceProperties dataSourceProperties) {
return dataSourceProperties.initializeDataSourceBuilder().build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy