我正在尝试为我的Spring Boot应用程序连接2个不同的数据库,并且发生此错误:
申请无法开始
说明:
在com.SyncFibertToolSpring.SyncFibertTool.MydbDB.Config.MydbDbConfig中,方法objectManager的参数0需要一个类型为'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder'的bean。
操作:
考虑在您的配置中定义类型为“ org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder”的bean。
下面您将找到我的两个数据库配置类
package com.SyncFibertToolSpring.SyncFibertTool.MydbDB.Config;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
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.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "mydbEntityManagerFactory",
basePackages = {"com.SyncFibertToolSpring.SyncFibertTool.MydbDB.DAO"})
public class MydbDbConfig {
@Primary
@Bean(name = "mydbDataSource")
@ConfigurationProperties(prefix = "spring.mydb.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "mydbEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder, @Qualifier("mydbDataSource") DataSource dataSource) {
return builder.dataSource(dataSource)
.packages("com.SyncFibertToolSpring.SyncFibertTool.MydbDB.entities").
build();
}
@Primary
@Bean(name = "mydbTransactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("mydbEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
package com.SyncFibertToolSpring.SyncFibertTool.fibertoolSyncDB.Config;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
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.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "fibertoolSyncEntityManagerFactory",
basePackages = {"com.SyncFibertToolSpring.SyncFibertTool.fibertoolSyncDB.DAO"})
public class FibertToolDbConfig {
@Primary
@Bean(name = "fibertoolSyncDataSource")
@ConfigurationProperties(prefix = "spring.fibertoolsync.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "fibertoolSyncEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("fibertoolSyncDataSource") DataSource dataSource) {
return builder.dataSource(dataSource)
.packages("com.SyncFibertToolSpring.SyncFibertTool.fibertoolSyncDB.entities").
build();
}
@Primary
@Bean(name = "fibertoolSyncTransactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("fibertoolSyncEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
答案 0 :(得分:2)
这里的第一个错误是您定义了两个@Primary
数据源。从两个@Primary
类之一中删除注释@Configuration
,然后将其留给一个类