java - 在 spring-boot 中使用 2 个数据库时出现问题。无法 Autowiring 。未找到 EntityManagerFactoryBuilder 的 beans

标签 java spring-boot

我正在尝试为 Spring Boot 使用 2 个数据库。但是,它说无法 Autowiring 。

No beans of EntityManagerFactoryBuild type found

我是 Spring Boot 新手,我正在使用 MySQL 8、Spring Boot 2 和 Java 12。

我在此处包含了学生和 adms 配置文件以及存储库文件。 文件结构为

com.project.attendance
        -configure
        -model
        -repository
        -dao
        -controller

StudentConfigure 类(class)配置

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = "com.project.attendance.repository.student",
        entityManagerFactoryRef = "studentEntityManager",
        transactionManagerRef = "studentTransactionManager"
)

public class StudentConfigure {

    @Primary
    @Bean(name="studentDataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource studentDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name="studentEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("studentDataSource") DataSource dataSource){
        return builder
                .dataSource(dataSource)
                .packages("com.project.attendance.dao.student")
                .persistentUnit("sampledb")
                .build();
    }

    @Primary
    @Bean(name="studentTransactionManager")
    public PlatformTransactionManager studentTransactionManager(
            @Qualifier("studentEntityManagerFactory") EntityManagerFactory studentEntityManagerFactory){
        return new JpaTransactionManager(studentEntityManagerFactory);
    }

}

AdmsConfigure配置类实现

  package com.project.attendance.configure;

    import javax.sql.DataSource;

    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(
            basePackages = {"com.project.attendance.repository.adms"},
            entityManagerFactoryRef = "admsEntityManager",
            transactionManagerRef = "admsTransactionManager"
    )
    @ComponentScan(basePackages = "com.project.attendance.repository.adms")

    public class AdmsConfigure {

        @Bean(name="admsDataSource")
        @ConfigurationProperties(prefix = "spring.datasource2")
        public DataSource admsDataSource(){
            return DataSourceBuilder.create().build();
        }

        @Bean(name="admsEntityManagerFactory")
        public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("admsDataSource") DataSource dataSource){
            return builder
                    .dataSource(dataSource)
                    .packages("com.project.attendance.dao.adms")
                    .persistentUnit("adms")
                    .build();
        }

        @Primary
        @Bean(name="admsTransactionManager")
        public PlatformTransactionManager admsTransactionManager(
                @Qualifier("admsEntityManagerFactory") EntityManagerFactory admsEntityManagerFactory){
            return new JpaTransactionManager(admsEntityManagerFactory);
        }
    }

Spring DATASOURCE(DataSourceAutoConfiguration 和 DataSourceProperties)

spring.datasource.url = jdbc:mysql://localhost:3306/sampledb?useSSL=false
spring.datasource.username = root
spring.datasource.password =root

spring.datasource2.url = jdbc:mysql://localhost:3306/adms?useSSL=false
spring.datasource2.username = root
spring.datasource2.password =root
logging.level.root=WARN

最佳答案

我认为问题在于,在 AdmsConfigure 类中,您将 admsEntityManager 引用为entityManagerFactoryRef。相反,您应该引用实际工厂而不是经理。

代码应该是这样的

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = {"com.project.attendance.repository.adms"},
        entityManagerFactoryRef = "admsEntityManagerFactory",
        transactionManagerRef = "admsTransactionManager"
)
@ComponentScan(basePackages = "com.project.attendance.repository.adms")
public class AdmsConfigure {

    @Bean(name="admsDataSource")
    @ConfigurationProperties(prefix = "spring.datasource2")
    public DataSource admsDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name="admsEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("admsDataSource") DataSource dataSource){
        return builder
                .dataSource(dataSource)
                .packages("com.project.attendance.dao.adms")
                .persistentUnit("adms")
                .build();
    }

    @Bean(name="admsTransactionManager")
    public PlatformTransactionManager admsTransactionManager(
            @Qualifier("admsEntityManagerFactory") EntityManagerFactory admsEntityManagerFactory){
        return new JpaTransactionManager(admsEntityManagerFactory);
    }
}

此外,您不应该使用 @Primary 注释来注释您的两个 PlatformTransactionManager。

关于java - 在 spring-boot 中使用 2 个数据库时出现问题。无法 Autowiring 。未找到 EntityManagerFactoryBuilder 的 beans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57800969/

相关文章:

java - 修改匿名类中的外部类属性

Java : Rename MultiPartFile with unique name and store it to a folder and save the url to database

spring - 使用 Rest 和 spring boot 将阿拉伯字符保存到数据库中

java - 在 spring-data-mongoDB 中创建 MongoTransactionManager bean 时出错

java - Tomcat 停止问题 + Spring boot + 服务

java比较两个枚举

java - 计算 Spark 数据集上的 Pearson 相关性

java - 如何在 OSX 上的 Java 7 中使用 32 位 native 库

mysql - Spring Boot应用程序启动失败

java - Spring Boot MSSQL Kerberos 身份验证