Spring Boot 2.0.0.M4 & Hibernate 5.2.11.Final 找不到 EntityManagerFactoryBuilder 类型的 bean
Spring Boot 2.0.0.M4 & Hibernate 5.2.11.Final could not find bean of type EntityManagerFactoryBuilder
我很清楚这似乎是一些配置问题,但我自己无法解决。我希望你们能帮助我?
None 我发现的示例表明必须为 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
})
我的 Pom
回应@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
})
通过排除 HibernateJpaAutoConfiguration
,您将关闭创建 EntityManagerFactoryBuilder
bean。
默认情况下 EntityManagerFactoryBuilder
bean 由 JpaBaseConfiguration#entityManagerFactoryBuilder(JpaVendorAdapter, ObjectProvider)
生成。
只有一个 JpaBaseConfiguration
实现 - HibernateJpaConfiguration
,在以下情况下激活:
- 只有一个
DataSource
候选人(或一个被标记为@Primary
);
HibernateJpaAutoConfiguration
条件为真 - 检查 HibernateJpaAutoConfiguration
class.
您应该在没有构建器的情况下创建和配置 LocalContainerEntityManagerFactoryBean(就像您在自己的答案中所做的那样),或者像这样在您的配置中手动创建 EntityManagerFactoryBuilder:
@Bean
public EntityManagerFactoryBuilder entityManagerFactoryBuilder() {
return new EntityManagerFactoryBuilder(new HibernateJpaVendorAdapter(), new HashMap<>(), null);
}
我很清楚这似乎是一些配置问题,但我自己无法解决。我希望你们能帮助我?
None 我发现的示例表明必须为 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
})
我的 Pom
回应@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
})
通过排除 HibernateJpaAutoConfiguration
,您将关闭创建 EntityManagerFactoryBuilder
bean。
默认情况下 EntityManagerFactoryBuilder
bean 由 JpaBaseConfiguration#entityManagerFactoryBuilder(JpaVendorAdapter, ObjectProvider)
生成。
只有一个 JpaBaseConfiguration
实现 - HibernateJpaConfiguration
,在以下情况下激活:
- 只有一个
DataSource
候选人(或一个被标记为@Primary
); HibernateJpaAutoConfiguration
条件为真 - 检查HibernateJpaAutoConfiguration
class.
您应该在没有构建器的情况下创建和配置 LocalContainerEntityManagerFactoryBean(就像您在自己的答案中所做的那样),或者像这样在您的配置中手动创建 EntityManagerFactoryBuilder:
@Bean
public EntityManagerFactoryBuilder entityManagerFactoryBuilder() {
return new EntityManagerFactoryBuilder(new HibernateJpaVendorAdapter(), new HashMap<>(), null);
}