blinkfox / fenix

This is an extension library to the Spring Data JPA complex or dynamic SQL query. 这是一个比 MyBatis 更加强大的 Spring Data JPA 扩展库,为解决复杂动态 JPQL (或 SQL) 而生。https://blinkfox.github.io/fenix
https://blinkfox.github.io/fenix
Apache License 2.0
346 stars 72 forks source link

多数据源 配置报错 #65

Closed qxwei closed 2 years ago

qxwei commented 2 years ago

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.

blinkfox commented 2 years ago

@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);
        }
    }

}
qxwei commented 2 years ago

这样配置可以

` @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());
}

}`

qxwei commented 2 years ago

已解决