小编典典

Spring Boot 2.0.0.M4和Hibernate 5.2.11.Final找不到EntityManagerFactoryBuilder类型的bean

spring-boot

我显然有一些配置问题,但我自己无法解决。我希望你们能帮助我吗?

我发现的所有示例都没有表明必须为EntityManagerFactoryBuilder创建bean,这是什么问题。

我正在尝试配置完全独立的数据源,包括不同的实体管理器等。

我的错误:


申请开始失败


描述:

方法entityManagerFactory的参数0需要找不到类型为“
org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder”的bean。

行动:

考虑在配置中定义类型为“
org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder”的bean。

我的数据源配置

package ...;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
public class DataSourceConfiguration {


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



    @Primary
    @Bean(name = "myEntityManager")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder factoryBuilder,
                                                                       @Qualifier("myEntityManager") DataSource bds) {
        ...
    }
}

我的应用程序

@SpringBootApplication(exclude = {
        DataSourceAutoConfiguration.class,
        HibernateJpaAutoConfiguration.class,
        JpaRepositoriesAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class
})

我的宝


阅读 746

收藏
2020-05-30

共1个答案

小编典典

回应@Albert Bos问题…

我想这是什么。

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackageClasses = { MyEntityRepository.class },
        repositoryBaseClass = EntityRepositoryImpl.class,
        transactionManagerRef = "myTransactionManager",
        entityManagerFactoryRef = "myEntityManagerFactory"
)
public class MyDataSourceConfiguration { ...

这是我的数据源配置。

@Bean(name = DATASOURCE_NAME + "DataSource")
public DataSource dataSource(@Qualifier(DATASOURCE_NAME + "DataSourceProperties") DataSourceProperties dataSourceProperties) 
{
    return DataSourceBuilder.create()
            .url(dataSourceProperties.getUrl())
            .username(dataSourceProperties.getUsername())
            .password(dataSourceProperties.getPassword())
            .driverClassName(dataSourceProperties.getDriverClassName())
            .build();
}

@Bean(name = DATASOURCE_NAME + "EntityManagerFactory")
@PersistenceContext(unitName = DATASOURCE_NAME + "PersistenceUnit")
public EntityManagerFactory entityManagerFactory(
        @Qualifier(DATASOURCE_NAME + "JpaProperties") Properties jpaProperties,
        @Qualifier(DATASOURCE_NAME + "DataSource") DataSource dataSource,
        @Qualifier(DATASOURCE_NAME + "JpaVendorAdapter") JpaVendorAdapter jpaVendorAdapter) 
{
    LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
    factoryBean.setDataSource(dataSource);
    factoryBean.setJpaProperties(jpaProperties);
    factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
    factoryBean.setPackagesToScan(MyEntity.class.getPackage().getName());
    factoryBean.setPersistenceUnitName(DATASOURCE_NAME + "PersistenceUnit");
    factoryBean.afterPropertiesSet();
    return factoryBean.getObject();
}

@Bean(name = DATASOURCE_NAME + "TransactionManager")
public PlatformTransactionManager transactionManager(@Qualifier(DATASOURCE_NAME + "EntityManagerFactory") EntityManagerFactory entityManagerFactory) 
{
    JpaTransactionManager txManager = new JpaTransactionManager();
    txManager.setEntityManagerFactory(entityManagerFactory);
    return txManager;
}

和应用程序配置

@SpringBootApplication(exclude = {
        LiquibaseAutoConfiguration.class,
        DataSourceAutoConfiguration.class,
        ValidationAutoConfiguration.class,
        HibernateJpaAutoConfiguration.class,
        JpaRepositoriesAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class
})
2020-05-30