0
我試圖合併使用ratpack.groovy中的ServerConfig
塊的服務器和數據庫配置,但嘗試創建數據源時postgresConfig
爲空。如何從Ratpack groovy的ServerConfig塊中綁定配置?
PostgresConfig.groovy
@Compile Static
class PostgresConfig {
String user
String password
String serverName
String databaseName
Integer portNumber
}
PostgresModule.groovy
@CompileStatic
class PostgresModule extends ConfigurableModule<PostgresConfig> {
@Override
protected void configure() {
}
@Provides
DataSource dataSource(final PostgresConfig config) {
createDataSource(config)
}
protected DataSource createDataSource(final PostgresConfig config) {
new PgSimpleDataSource(
user: config.user,
password: config.password,
serverName: config.serverName,
databaseName: config.databaseName,
portNumber: config.portNumber
)
}
}
ratpack.groovy
ratpack {
serverConfig {
props([
'postgres.user': 'username',
'postgres.password': 'password',
'postgres.serverName': 'localhost',
'postgres.databaseName': 'postgres',
'postgres.portNumber': 5432
] as Map<String, String>)
yaml "config.yaml"
env()
sysProps()
require("/postgres", PostgresConfig)
}
bindings {
PostgresConfig postgresConfig
module HikariModule, { HikariConfig config ->
config.dataSource = new PostgresModule().dataSource(postgresConfig)
}
}
}