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

多数据源配置报错,A component required a bean named 'entityManagerFactory' that could not be found. #83

Closed HongyiPaulZeng closed 1 year ago

HongyiPaulZeng commented 1 year ago

数据源1: @Configuration @EnableTransactionManagement @EnableJpaRepositories(entityManagerFactoryRef = "primaryEntityManagerFactory", // transactionManagerRef = "primaryTransactionManager", // basePackages = {"com.example.iap.two"}) // 设置Repository所在位置 public class PrimaryConfig {

@Primary
@Bean(name = "primaryDataSource")
@Qualifier(value = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().type(HikariDataSource.class).build();
}

@Primary
@Bean(name = "primaryEntityManager")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
    return primaryEntityManagerFactory(builder).getObject().createEntityManager();
}

@Primary
@Bean(name = "primaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(EntityManagerFactoryBuilder builder) {
    return builder
            .dataSource(primaryDataSource())
            .packages("com.example.iap.two")
            .persistenceUnit("primaryPersistenceUnit")
            .build();
}

@Primary
@Bean(name = "primaryTransactionManager")
public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
    return new JpaTransactionManager(primaryEntityManagerFactory(builder).getObject());
}

}

数据源2: @Configuration @EnableTransactionManagement @EnableJpaRepositories(entityManagerFactoryRef = "secondaryEntityManagerFactory", // transactionManagerRef = "secondaryTransactionManager", // basePackages = { "com.example.iap.two" }) // 设置Repository所在位置 public class SecondaryConfig {

@Bean(name = "sencondaryDataSource")
@Qualifier(value = "sencondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource sencondaryDataSource() {
    return DataSourceBuilder.create().type(HikariDataSource.class).build();
}

@Bean(name = "secondaryEntityManager")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
    return secondaryEntityManagerFactory(builder).getObject().createEntityManager();
}

@Bean(name = "secondaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory(EntityManagerFactoryBuilder builder) {
    return builder//
            .dataSource(sencondaryDataSource()) //
            .packages("com.example.iap.two")//
            .persistenceUnit("secondaryPersistenceUnit")//
            .build();
}

@Bean(name = "secondaryTransactionManager")
PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
    return new JpaTransactionManager(secondaryEntityManagerFactory(builder).getObject());
}

}

当不启用fenix时是正常的。 当增加@EnableFenix 后。两个配置文件改为: @EnableFenix(entityManagerFactoryRef = "primaryEntityManagerFactory", // transactionManagerRef = "primaryTransactionManager", // basePackages = {"com.example.iap.two"}) // 设置Repository所在位置 后启动一直报错 A component required a bean named 'entityManagerFactory' that could not be found.

参考之前的issue也没能解决。请教各位大大这种错误是哪个地方配置有问题

HongyiPaulZeng commented 1 year ago

已解决。这里的坑是不能再启动类使用@EnableFenix