@Configuration@EnableJpaRepositories(basePackageClasses=CustomerRepository.class,entityManagerFactoryRef="customerEntityManager",transactionManagerRef="customerTransactionManager")publicclassDataSourceConfiguration{@Autowired(required=false)privatePersistenceUnitManagerpersistenceUnitManager;@Bean@ConfigurationProperties("app.customer.jpa")publicJpaPropertiescustomerJpaProperties(){returnnewJpaProperties();}@Bean@ConfigurationProperties("app.customer.jpa.hibernate")publicHibernatePropertiescustomerHibernateProperties(){returnnewHibernateProperties();}@Bean@ConfigurationProperties("app.customer.development.datasource")publicDataSourcePropertiesdevelopmentDataSourceProperties(){returnnewDataSourceProperties();}@BeanpublicDataSourcedevelopmentDataSource(){returndevelopmentDataSourceProperties().initializeDataSourceBuilder().build();}@Bean@ConfigurationProperties("app.customer.testing.datasource")publicDataSourcePropertiestestingDataSourceProperties(){returnnewDataSourceProperties();}@BeanpublicDataSourcetestingDataSource(){returntestingDataSourceProperties().initializeDataSourceBuilder().build();}@Bean@ConfigurationProperties("app.customer.production.datasource")publicDataSourcePropertiesproductionDataSourceProperties(){returnnewDataSourceProperties();}@BeanpublicDataSourceproductionDataSource(){returntestingDataSourceProperties().initializeDataSourceBuilder().build();}/**
* Adds all available datasources to datasource map.
*
* @return datasource of current context
*/@Bean@PrimarypublicDataSourcecustomerDataSource(){DataSourceRouterrouter=newDataSourceRouter();finalHashMap<Object,Object>map=newHashMap<>(3);map.put(DatabaseEnvironment.DEVELOPMENT,developmentDataSource());map.put(DatabaseEnvironment.TESTING,testingDataSource());map.put(DatabaseEnvironment.PRODUCTION,productionDataSource());router.setTargetDataSources(map);returnrouter;}@BeanpublicLocalContainerEntityManagerFactoryBeancustomerEntityManager(@Qualifier("customerJpaProperties")finalJpaPropertiescustomerJpaProperties){EntityManagerFactoryBuilderbuilder=createEntityManagerFactoryBuilder(customerJpaProperties);returnbuilder.dataSource(customerDataSource()).packages(Customer.class).properties(customerHibernateProperties().determineHibernateProperties(customerJpaProperties.getProperties(),newHibernateSettings())).persistenceUnit("customerEntityManager").build();}@Bean@PrimarypublicJpaTransactionManagercustomerTransactionManager(@Qualifier("customerEntityManager")finalEntityManagerFactoryfactory){returnnewJpaTransactionManager(factory);}privateEntityManagerFactoryBuildercreateEntityManagerFactoryBuilder(finalJpaPropertiescustomerJpaProperties){JpaVendorAdapterjpaVendorAdapter=createJpaVendorAdapter(customerJpaProperties);returnnewEntityManagerFactoryBuilder(jpaVendorAdapter,customerJpaProperties.getProperties(),persistenceUnitManager);}privateJpaVendorAdaptercreateJpaVendorAdapter(JpaPropertiesjpaProperties){AbstractJpaVendorAdapteradapter=newHibernateJpaVendorAdapter();adapter.setShowSql(jpaProperties.isShowSql());adapter.setGenerateDdl(jpaProperties.isGenerateDdl());returnadapter;}}