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!特别是链接。谢谢丹!特别是对于链接。