如何使用带参数的自定义驱动程序类构建数据库数据源?

huangapple go评论86阅读模式
英文:

How to construct a DB DataSource using parameterized custom driver class?

问题

在Spring Boot应用程序中,我正在连接到一个Postgres数据库。根据特定的要求,我需要使用一个参数化的DriverClass,它是org.postgresql.Driver的子类。

如何使用参数化的driverClass构建DataSource对象?

DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("com.apps.CustomPostgresqlDriver") // 不确定如何将字符串参数传递给其构造函数
.build();

自定义的驱动程序com.apps.CustomPostgresqlDriver定义如下。

public class CustomPostgresqlDriver implements Driver {

   public CustomPostgresqlDriver(String appParam){
      ....
   }
   ...
}

我没有看到任何选项可以使用上述的单参数构造函数实例化(或让Spring实例化)驱动程序。示例:

new CustomPostgresqlDriver("my-app-db-param")
英文:

In a Spring Boot app, I am connecting to a Postgres DB. For a specific requirement, I need to use a parameterized DriverClass which is a subclass of org.postgresql.Driver.

How can I construct the DataSource object using a parameterized driverClass ?

DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("com.apps.CustomPostgresqlDriver") // Not sure how to pass a string argument to its constructor
.build()

The customer driver com.apps.CustomPostgresqlDriver is defined like this.

Public class CustomPostgresqlDriver implements Driver {

   public void CustomPostgresqlDriver(String appParam){

      ....
   }
...

}

I don't see any option to instantiate (or let Spring instantiate) the driver with the above one-arg constructor. Example:

new CustomPostgresqlDriver("my-app-db-param")

答案1

得分: 1

你可以使用 DataSourceBuilder 完成这个任务:
首先,在 application.yml 中配置两个不同的数据源设置,如下所示:

spring:
  datasource:
    main: 
      driver-class-name: ${ds.driver}
      url: ${ds.url}
      username: ${ds.username}
      password: ${ds.password}
      hikari:
        maximum-pool-size: ${ds.maximum.pool.size}
        minimum-idle: ${ds.minimum.idle}
        connection-timeout: 10000
        connection-test-query: SELECT 1
    read:
      driver-class-name: ${ds.driver}
      url: ${ds.read.url}
      username: ${ds.read.username}
      password: ${ds.read.password}
      hikari:
        maximum-pool-size: ${ds.read.maximum.pool.size}
        minimum-idle: ${ds.read.minimum.idle}
        connection-timeout: 10000
        connection-test-query: SELECT 1

其次,使用以下代码通过 DataSourceBuilder 注入它们:

import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;

@Primary
@Bean
@ConfigurationProperties("spring.datasource.main")
public DataSource main() {
    return DataSourceBuilder.create().build();
}
 
@Bean
@ConfigurationProperties("spring.datasource.read")
public DataSource read() {
    return DataSourceBuilder.create().build();
}
英文:

You can use DataSourceBuilder to finish this task:
The first config two different ds setting in application.yml like as below:

spring:
  datasource:
    main: 
      driver-class-name: ${ds.driver}
      url: ${ds.url}
      username: ${ds.username}
      password: ${ds.password}
      hikari:
        maximum-pool-size: ${ds.maximum.pool.size}
        minimum-idle: ${ds.minimum.idle}
        connection-timeout: 10000
        connection-test-query: SELECT 1
    read:
      driver-class-name: ${ds.driver}
      url: ${ds.read.url}
      username: ${ds.read.username}
      password: ${ds.read.password}
      hikari:
        maximum-pool-size: ${ds.read.maximum.pool.size}
        minimum-idle: ${ds.read.minimum.idle}
        connection-timeout: 10000
        connection-test-query: SELECT 1

The second inject them with DataSourceBuilder as below:


import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;

	@Primary
	@Bean
    @ConfigurationProperties("spring.datasource.main")
    public DataSource main() {
        return DataSourceBuilder.create().build();
    }
 
 
    @Bean
    @ConfigurationProperties("spring.datasource.read")
    public DataSource read() {
        return DataSourceBuilder.create().build();
    }

huangapple
  • 本文由 发表于 2020年10月10日 07:34:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64288534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定