我清楚地看到了一些配置问题,但我自己无法解决这个问题。我希望你们能帮助我吗?
我发现的所有示例都没有表明必须为EntityManagerFactoryBuilder创建一个bean,所以问题是什么。
我正在尝试配置完全独立的数据源,包括不同的实体管理器等。
我的错误:
申请失败
说明
方法entityManagerFactory的参数0需要一个类型为' org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder'的bean。无法找到。
动作:
考虑定义一个类型为' org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder'的bean。在你的配置中。
我的DataSourceConfiguration
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
答案 0 :(得分:3)
通过排除HibernateJpaAutoConfiguration
,您将关闭创建EntityManagerFactoryBuilder
bean。
默认情况下,EntityManagerFactoryBuilder
的bean由JpaBaseConfiguration#entityManagerFactoryBuilder(JpaVendorAdapter, ObjectProvider)
产生。
只有一个JpaBaseConfiguration
实现-HibernateJpaConfiguration
,在以下情况下会被激活:
DataSource
候选人(或一个被标记为@Primary
); HibernateJpaAutoConfiguration
条件为真-检查HibernateJpaAutoConfiguration
类。您应该创建并配置不带构建器的LocalContainerEntityManagerFactoryBean(就像您在自己的答案中所做的那样),或者在配置中手动创建EntityManagerFactoryBuilder,如下所示:
@Bean
public EntityManagerFactoryBuilder entityManagerFactoryBuilder() {
return new EntityManagerFactoryBuilder(new HibernateJpaVendorAdapter(), new HashMap<>(), null);
}
答案 1 :(得分: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
})