You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot多数据源配置中EntityManagerFactoryBuilder无法自动注入的问题解决求助

Spring Boot多数据源配置中EntityManagerFactoryBuilder无法自动注入的问题解决求助

大家好,我正在尝试在单个Spring Boot应用中配置两个PostgreSQL数据库(包含一个向量数据库),所以为Postgres主库和向量库分别编写了配置类。以下是主库的配置代码:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    basePackages = "com.example.sse_chat_bot.repository.primary",
    entityManagerFactoryRef = "primaryEntityManagerFactory",
    transactionManagerRef = "primaryTransactionManager"
)
public class PrimaryDataSourceConfig {

    @Bean(name = "primaryDataSourceProperties")
    @ConfigurationProperties("spring.datasource.primary")
    public DataSourceProperties dataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "primaryDataSource")
    public DataSource dataSource() {
        return dataSourceProperties().initializeDataSourceBuilder().build();
    }

    // The streamlined, modern approach - no separate builder bean needed
    @Bean(name = "primaryEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        EntityManagerFactoryBuilder builder,
        @Qualifier("primaryDataSource") DataSource dataSource
    ) {
        return builder
            .dataSource(dataSource) // Set the DataSource
            .packages("com.example.sse_chat_bot.entity.primary")
            .build();
    }

    @Bean(name = "primaryTransactionManager")
    public PlatformTransactionManager transactionManager(
        @Qualifier("primaryEntityManagerFactory") LocalContainerEntityManagerFactoryBean entityManagerFactory
    ) {
        return new JpaTransactionManager(entityManagerFactory.getObject());
    }
}

但启动应用时直接失败了,错误信息如下:

***************************
APPLICATION FAILED TO START
***************************

Description:
Parameter 0 of method entityManagerFactory in com.example.sse_chat_bot.config.PrimaryDataSourceConfig required a bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' that could not be found.

Action:
Consider defining a bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' in your configuration.

现在Spring无法自动注入EntityManagerFactoryBuilder这个Bean,想请教下各位,这个问题该怎么解决呀?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 08:23:09