我正在尝试配置两个jpa数据源,我和this example一样,每件事都很好但是可以自动完成自动配置,只需添加一个新配置而不必创建{{1手动。
LocalContainerEntityManagerFactoryBean
答案 0 :(得分:5)
我不确定您的具体问题是什么或者您想要完成什么,但我会向您展示我是如何使用自动将Spring数据源与Spring Boot一起使用的配置功能:
为每个数据源创建配置(在单独的类中仅为了更好的阅读):
PrimaryDbConfig.java
C:\Users\User\uabshp\bower_components\UglifyJS2\bin\uglifyjs:111
.array("reserved-file")
^
TypeError: yargs.usage(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).describe(...).alias(...).alias(...).alias(...).alias(...).alias(...).alias(...).alias(...).alias(...).alias(...).alias(...).alias(...).string(...).string(...).string(...).string(...).string(...).string(...).string(...).string(...).string(...).string(...).string(...).string(...).string(...).string(...).string(...).string(...).string(...).string(...).array is not a function
at Object.<anonymous> (C:\Users\User\uabshp\bower_components\UglifyJS2\bin\uglifyjs:111:6)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
at run (bootstrap_node.js:352:7)
at startup (bootstrap_node.js:144:9)
at bootstrap_node.js:467:3
SecondaryDbConfig.java
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.aoc.dao", entityManagerFactoryRef = "entityManager", transactionManagerRef = "transactionManager")
public class PrimaryDBConfiguration {
@Bean(name = "dataSource")
@Primary
@ConfigurationProperties(prefix = "primary.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@PersistenceContext(unitName = "primary")
@Primary
@Bean(name = "entityManager")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder.dataSource(dataSource())
.persistenceUnit("primary")
.packages("com.aoc.model")
.build();
}
}
使用它@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.aoc.dao", entityManagerFactoryRef = "secondaryEntityManager", transactionManagerRef = "secondaryTransactionManager")
public class SecondaryDBConfiguration {
@Bean
@ConfigurationProperties(prefix = "secondary.datasource")
public DataSource secDataSource() {
return DataSourceBuilder.create().build();
}
@PersistenceContext(unitName = "secondary")
@Bean(name = "secondaryEntityManager")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder.dataSource(secDataSource())
.persistenceUnit("secondary")
.packages("com.aoc.siri")
.build();
}
@Bean(name = "secondaryTransactionManager")
public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
JpaTransactionManager tm = new JpaTransactionManager();
tm.setEntityManagerFactory(entityManagerFactory(builder).getObject());
tm.setDataSource(secDataSource());
return tm;
}
}
PersistenceContext
分配每个DAO:
OneDaoImpl.java
unitName
AnotherDaoImpl.java
@Repository(value = "OneDaoImpl")
public class OneDaoImpl {
@PersistenceContext(unitName="primary")
private EntityManager manager;
public List<AplicacioMsc> getAllOne() {
return (List<One>) manager.createQuery("FROM ONE", One.class).getResultList();
}
}
然后在@Repository(value = "anotherDaoImpl")
public class AnotherDaoImpl {
@PersistenceContext(unitName = "secondary")
private EntityManager manager;
public List<Producte> getAllAnother() {
return (List<Another>) manager.createQuery("FROM Another", Another.class).getResultList();
}
}
src/main/resources/application.properties
您可能知道如果您使用recommended Spring Boot packages naming这些类和语言,您应该能够同时使用这两个dataSource。