2016-09-04 2 views
0

現在のHikariModuleには、Javaコードでハードコードされた値が含まれていますが、それは良い方法ではありませんが、db.propertiesで定義された使用値です。これを達成する方法は?カスタムConfigurableModule<MyModule.Settings>を作成し、HikariModuleMyModuleに登録する必要がありますか?モジュールの中にモジュールを登録する方法を見つけられませんでした。ありがとう!アプリケーション構成を使用してRatpackのConfigurableModuleを登録する方法

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 
        ... 
      ) 
     ); 
    } 
} 

答えて

2

のは、あなたがその内容ですsrc/ratpack/postgres.yamlpostgres.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(...)); 

完全な実施例がここにhttps://github.com/danhyun/modern-java-web

+0

おかげでダンあります:あなたがこれを行うことができ、あなたのメインクラスから

!特にリンクのために。 –

関連する問題