cod*_*eek 5 spring-data spring-data-jpa spring-boot
我想在我的 SpringBoot(版本 2.4)应用程序中配置两个数据源。
下面是application.properties:
spring.datasource2.url=jdbc:postgresql://localhost:5432/db1
spring.datasource2.username=//username
spring.datasource2.password=//pwd
spring.datasource2.initialization-mode=always
spring.datasource2.driver-class-name=org.postgresql.Driver
spring.datasource1.url=jdbc:postgresql://localhost:5432/db2
spring.datasource1.username=//usrename
spring.datasource1.password=//pwd
spring.datasource1.initialization-mode=always
spring.datasource1.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.hibernate.show-sql=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.id.new_generator_mappings=false
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.h2.console.enabled=true
spring.datasource.continue-on-error=true
spring.servlet.multipart.max-file-size=50MB
spring.servlet.multipart.max-request-size=50MB
project.http.enable=false
project.https.port=9090
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
server.os=LINUX
dl.writeTOFile=false
dl.writeTOConsole=true
Run Code Online (Sandbox Code Playgroud)
下面是我的 pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- marked the embedded servlet container as provided -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.4.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
当我运行该应用程序时,出现以下错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Run Code Online (Sandbox Code Playgroud)
当我将一个数据源添加为 Spring.datasource,将另一个数据源添加为 Spring.datasource1 时,应用程序启动正常,但它仅查看在 Spring.datasource 中配置的数据库。
以下是 DB1 Config 类:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories( entityManagerFactoryRef = "DB1EntityManagerFactory", transactionManagerRef = "DB1TransactionManager", basePackages = {
"com.datasource1.repository" } )
@EntityScan("com.datasource1.entity" )
public class Db1Config {
@Primary
@Bean
@ConfigurationProperties("spring.datasource")
public DataSourceProperties SourceProperties() {
return new DataSourceProperties();
}
@Primary
@Bean( name = "DB1Datasource" )
@ConfigurationProperties( prefix = "spring.datasource.configuration" )
public DataSource dataSource()
{
return SourceProperties().initializeDataSourceBuilder()
.type(HikariDataSource.class).build();
}
@Primary
@Bean( name = "DB1EntityManagerFactory" )
public LocalContainerEntityManagerFactoryBean barEntityManagerFactory( EntityManagerFactoryBuilder builder,
@Qualifier( "DB1Datasource" ) DataSource dataSource )
{
return builder.dataSource(dataSource).packages("com.datasource1.entity")
.persistenceUnit("db1").build();
}
@Primary
@Bean( name = "DB1TransactionManager" )
public PlatformTransactionManager barTransactionManager(
@Qualifier( "DB1EntityManagerFactory" ) EntityManagerFactory barEntityManagerFactory )
{
return new JpaTransactionManager(barEntityManagerFactory);
}
}
Run Code Online (Sandbox Code Playgroud)
以下是 DB2 配置:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories( entityManagerFactoryRef = "DB2EntityManagerFactory", transactionManagerRef = "DB2TransactionManager", basePackages = {
"com.DB2.repository" } )
@EntityScan( "com.DB2.entity" )
public class Db2Config {
@Bean
@ConfigurationProperties("spring.datasource2")
public DataSourceProperties DB2SourceProperties() {
return new DataSourceProperties();
}
@Bean( name = "DB2Datasource" )
@ConfigurationProperties( prefix = "spring.datasource2.configuration" )
public DataSource dataSource()
{
return DB2SourceProperties().initializeDataSourceBuilder()
.type(HikariDataSource.class).build();
}
@Bean( name = "DB2EntityManagerFactory" )
public LocalContainerEntityManagerFactoryBean barEntityManagerFactory( EntityManagerFactoryBuilder builder,
@Qualifier( "DB2Datasource" ) DataSource dataSource )
{
return builder.dataSource(dataSource).packages("com.DB2.entity")
.persistenceUnit("db2").build();
}
@Bean( name = "DB2TransactionManager" )
public PlatformTransactionManager barTransactionManager(
@Qualifier( "DB2EntityManagerFactory" ) EntityManagerFactory barEntityManagerFactory )
{
return new JpaTransactionManager(barEntityManagerFactory);
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
你应该改变
spring.datasource1.url=jdbc:postgresql://localhost:5432/db2
Run Code Online (Sandbox Code Playgroud)
到
spring.datasource1.jdbc-url=jdbc:postgresql://localhost:5432/db2
Run Code Online (Sandbox Code Playgroud)
不知道为什么会出现这种情况,文档也没有这么说,但我遇到了这个问题,这对我来说很有效
编辑: 作为@M。Deinum 在他的评论中提到:这些
@ConfigurationProperties("spring.datasource")
(...)
@ConfigurationProperties( prefix = "spring.datasource.configuration" )
Run Code Online (Sandbox Code Playgroud)
应该改为
@ConfigurationProperties("spring.datasource1")
(...)
@ConfigurationProperties( prefix = "spring.datasource1.configuration" )
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26953 次 |
| 最近记录: |