多数据源 配置报错
qxwei opened this issue · 3 comments
The bean 'userRepository', defined in com.shares.repository.UserRepository defined in @EnableFenix declared on SharesApplication, could not be registered. A bean with that name has already been defined in com.shares.repository.UserRepository defined in @EnableJpaRepositories declared on PrimaryConfig and overriding is disabled.
@qxwei 估计你的多数据源配置有问题。下面是一个我们的配置示例,你大概可以参考下:
Aaa 数据源的配置类:
@Configuration
@EnableTransactionManagement
@EnableFenix(
entityManagerFactoryRef = "aaaEntityManagerFactory",
transactionManagerRef = "aaaTransactionManager",
basePackages = {"com.blinkfox.yourpackage.aaa"}
)
public class AaaDbConfig {
@Primary
@Bean(name = "aaaEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean aaaEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
// 设置数据源相关的参数
....
.build();
}
@Primary
@Bean(name = "aaaTransactionManager")
public PlatformTransactionManager aaaTransactionManager(EntityManagerFactoryBuilder builder) {
EntityManagerFactory entityManagerFactory = aaaEntityManagerFactory(builder).getObject();
if (Objects.nonNull(entityManagerFactory)) {
return new JpaTransactionManager(entityManagerFactory);
}
}
}
Bbb 数据源的配置类:
@Configuration
@EnableTransactionManagement
@EnableFenix(
entityManagerFactoryRef = "bbbEntityManagerFactory",
transactionManagerRef = "bbbTransactionManager",
basePackages = {"com.blinkfox.yourpackage.bbb"}
)
public class BbbDbConfig {
@Primary
@Bean(name = "bbbEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean bbbEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
// 设置数据源相关的参数
....
.build();
}
@Primary
@Bean(name = "bbbTransactionManager")
public PlatformTransactionManager bbbTransactionManager(EntityManagerFactoryBuilder builder) {
EntityManagerFactory entityManagerFactory = bbbEntityManagerFactory(builder).getObject();
if (Objects.nonNull(entityManagerFactory)) {
return new JpaTransactionManager(entityManagerFactory);
}
}
}
这样配置可以
`
@EnableFenix
@configuration
@EnableTransactionManagement
@EnableJpaRepositories(
repositoryFactoryBeanClass = FenixJpaRepositoryFactoryBean.class,
entityManagerFactoryRef="entityManagerFactoryPrimary",
transactionManagerRef="transactionManagerPrimary",
basePackages= { "com.shares.repository" }) //设置Repository所在位置
public class PrimaryConfig {
@Autowired
private DataSource primaryDataSource;
@Autowired
private JpaProperties jpaProperties;
@Autowired
private HibernateProperties hibernateProperties;
private Map<String, Object> getVendorProperties() {
return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
}
@Primary
@Bean(name = "entityManagerPrimary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
}
@Primary
@Bean(name = "entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
return builder.dataSource(primaryDataSource)
.packages("com.shares.model.entity") //设置实体类所在位置
.persistenceUnit("primaryPersistenceUnit")
.properties(getVendorProperties())
.build();
}
@Primary
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}`
`@EnableFenix
@configuration
@EnableTransactionManagement
@EnableJpaRepositories(
repositoryFactoryBeanClass = FenixJpaRepositoryFactoryBean.class,
entityManagerFactoryRef="entityManagerFactorySecondary",
transactionManagerRef="transactionManagerSecondary",
basePackages= { "com.shares.stockRepo"}) //设置Repository所在位置
public class SecondaryConfig {
@Autowired
private DataSource secondaryDataSource;
@Autowired
private JpaProperties jpaProperties;
@Autowired
private HibernateProperties hibernateProperties;
private Map<String, Object> getVendorProperties() {
return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
}
@Bean(name = "entityManagerSecondary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
}
@Bean(name = "entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
return builder.dataSource(secondaryDataSource)
.packages("com.shares.model.stockEntity") //设置实体类所在位置
.persistenceUnit("secondaryPersistenceUnit")
.properties(getVendorProperties())
.build();
}
@Bean(name = "transactionManagerSecondary")
public PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
}
}`
已解决