如何从Ratpack groovy中的ServerConfig块绑定配置?
我正在尝试使用ratpack.groovy中的如何从Ratpack groovy中的ServerConfig块绑定配置?,groovy,ratpack,Groovy,Ratpack,我正在尝试使用ratpack.groovy中的ServerConfig块合并服务器和数据库配置,但在尝试创建数据源时postgresConfig为空 PostgresConfig.groovy @Compile Static class PostgresConfig { String user String password String serverName String databaseName Integer portNumber } @Compi
ServerConfig
块合并服务器和数据库配置,但在尝试创建数据源时postgresConfig
为空
PostgresConfig.groovy
@Compile Static
class PostgresConfig {
String user
String password
String serverName
String databaseName
Integer portNumber
}
@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 {
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)
}
}
}
PostgresModule.groovy
@Compile Static
class PostgresConfig {
String user
String password
String serverName
String databaseName
Integer portNumber
}
@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 {
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)
}
}
}
@CompileStatic
类PostgresModule扩展了ConfigurableModule{
@凌驾
受保护的void configure(){
}
@提供
数据源数据源(最终PostgresConfig配置){
createDataSource(配置)
}
受保护的数据源createDataSource(最终PostgresConfig配置){
新的PgSimpleDataSource(
用户:config.user,
密码:config.password,
serverName:config.serverName,
databaseName:config.databaseName,
portNumber:config.portNumber
)
}
}
ratpack.groovy
@Compile Static
class PostgresConfig {
String user
String password
String serverName
String databaseName
Integer portNumber
}
@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 {
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)
}
}
}
ratpack{
服务器配置{
道具([
'postgres.user':'username',
“postgres.password”:“password”,
'postgres.serverName':'localhost',
'postgres.databaseName':'postgres',
“postgres.portNumber”:5432
](如图所示)
yaml“config.yaml”
环境()
sysProps()
要求(“/postgres”,PostgresConfig)
}
绑定{
PostgresConfig PostgresConfig
模块HIKARIMODEL,{HikariConfig配置->
config.dataSource=new PostgresModule().dataSource(postgresConfig)
}
}
}
在绑定
块中,您可以引用服务器配置
配置,从而获得已配置的PostgresConfig
。在您的用例中,您不需要require(“/postgres”,PostgresConfig)
语句
您可以使PostgresModule
类不扩展ConfigurableModule
,因为它没有用作模块
ratpack {
serverConfig { ... }
bindings {
module HikariModule, { HikariConfig config ->
config.dataSource = new PostgresModule().dataSource(serverConfig.get("/postgres", PostgresConfig)
}
}
}