我正在尝试在springBoot 2中创建多个mysql数据源,但出现错误。
我已经在application.properties中配置了2个数据源,并通过引用分别创建了两个配置类 “ https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7”
我的application.properties
#----- database 1 - customer -----#
spring.datasource.url=jdbc:mysql://localhost:3306/customers?useSSL=false
spring.datasource.username=root
spring.datasource.password=ram123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
#----- database 2 = forum ------#
spring.forum.datasource.url=jdbc:mysql://localhost:3306/forum?useSSL=false
spring.forum.datasource.username=root
spring.forum.datasource.password=ram123
spring.forum.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.forum.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
--------------
My configuration Class
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "customerEntityManagerFactory" ,
transactionManagerRef = "customerTransactionManager",
basePackages = { "io.habitate.repository.customer" }
)
public class CustomerDbConfig extends HikariConfig {
@Primary
@Bean(name = "customerDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return new HikariDataSource(this);
}
@Primary
@Bean(name = "customerEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
customerEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("customerDataSource") DataSource dataSource
) {
return
builder
.dataSource(dataSource)
.packages("io.habitate.model.customer")
.persistenceUnit("customers")
.build();
}
@Primary
@Bean(name = "customerTransactionManager")
public PlatformTransactionManager customerTransactionManager(
@Qualifier("customerEntityManagerFactory") EntityManagerFactory
customerEntityManagerFactory
) {
return new JpaTransactionManager(customerEntityManagerFactory);
}
}
ERROR 37348 --- [ restartedMain] com.zaxxer.hikari.HikariConfig : HikariPool-1 - dataSource or dataSourceClassName or jdbcUrl is required.
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'forumEntityManagerFactory' defined in class path resource [io/habitate/config/datasource/ForumDbConfig.class]: Unsatisfied dependency expressed through method 'forumEntityManagerFactory' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'forumDataSource' defined in class path resource [io/habitate/config/datasource/ForumDbConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.