Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何注册Ratpack';s ConfigurableModule使用应用程序配置_Java_Ratpack - Fatal编程技术网

Java 如何注册Ratpack';s ConfigurableModule使用应用程序配置

Java 如何注册Ratpack';s ConfigurableModule使用应用程序配置,java,ratpack,Java,Ratpack,当前的HikariModule包含Java代码中的硬编码值,这不是一个好的做法,最好使用db.properties中定义的值。如何做到这一点?我是否需要在MyModule内部创建自定义ConfigurableModule并注册HikariModule?我还没有找到在模块中注册模块的方法。谢谢 public class App { public static void main(String[] args) throws Exception { RatpackServer

当前的
HikariModule
包含Java代码中的硬编码值,这不是一个好的做法,最好使用
db.properties
中定义的值。如何做到这一点?我是否需要在
MyModule
内部创建自定义
ConfigurableModule
并注册
HikariModule
?我还没有找到在模块中注册模块的方法。谢谢

public class App {

    public static void main(String[] args) throws Exception {
        RatpackServer.start(s -> s 
             .serverConfig( configBuilder -> configBuilder
                .findBaseDir()
                .props("db.properties")
                .require("/database", Settings.class)
             )
             .registry(Guice.registry( bindings -> bindings
                     .module(HikariModule.class, hm -> {
                         hm.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
                         hm.addDataSourceProperty("url", "jdbc:postgresql://localhost:5433/ratpack");
                         hm.setUsername("postgres");
                         hm.setPassword("postgres");
                     }).bind(DatabaseInit.class)
             ))
             .handlers( chain -> chain
                    ...
             )
        ); 
    }
}

假设您在
src/ratpack/postgres.yaml
中有一个
postgres.yaml
文件,其内容如下:

db:
  dataSourceClassName: org.postgresql.ds.PGSimpleDataSource
  username: postgres
  password: password
  dataSourceProperties:
    databaseName: modern
    serverName: 192.168.99.100
    portNumber: 5432
在同一个目录中,假设您有一个空的
.ratpack
文件

从主类中,您可以执行以下操作:

RatpackServer.start(serverSpec -> serverSpec
      .serverConfig(config -> config
        .baseDir(BaseDir.find()) // locates the .ratpack file
        .yaml("postgres.yaml") // finds file relative to directory containing .ratpack file
        .require("/db", HikariConfig.class) // bind props from yaml file to HikariConfig class
      ).registry(Guice.registry(bindings -> bindings
        .module(HikariModule.class) // this will use HikariConfig to configure the module
      )).handlers(...));

这里有一个完整的工作示例

假设您在
src/ratpack/postgres.yaml
中有一个
postgres.yaml
文件,其内容如下:

db:
  dataSourceClassName: org.postgresql.ds.PGSimpleDataSource
  username: postgres
  password: password
  dataSourceProperties:
    databaseName: modern
    serverName: 192.168.99.100
    portNumber: 5432
在同一个目录中,假设您有一个空的
.ratpack
文件

从主类中,您可以执行以下操作:

RatpackServer.start(serverSpec -> serverSpec
      .serverConfig(config -> config
        .baseDir(BaseDir.find()) // locates the .ratpack file
        .yaml("postgres.yaml") // finds file relative to directory containing .ratpack file
        .require("/db", HikariConfig.class) // bind props from yaml file to HikariConfig class
      ).registry(Guice.registry(bindings -> bindings
        .module(HikariModule.class) // this will use HikariConfig to configure the module
      )).handlers(...));

这里有一个完整的工作示例

谢谢Dan!特别是链接。谢谢丹!特别是对于链接。