配置取决于启动模式
Configuration depending on launch mode
Play 可以在开发模式(通过 run
)、生产模式(通过 start
)或测试模式下启动。有没有办法根据启动模式提供不同的配置文件 (conf/application.conf
)?
您可以使用 play 提供的 3 种方式之一设置不同的配置文件:
1 - Using -Dconfig.resource
It will search for an alternative configuration file in the
application classpath (you usually provide these alternative
configuration files into your application conf/ directory before
packaging). Play will look into conf/ so you don’t have to add conf/.
$ /path/to/bin/ -Dconfig.resource=prod.conf
2 - Using -Dconfig.file
You can also specify another local configuration file not packaged
into the application artifacts:
$ /path/to/bin/ -Dconfig.file=/opt/conf/prod.conf
3 - Using -Dconfig.url
You can also specify a configuration file to be loaded from any URL:
$ /path/to/bin/
-Dconfig.url=http://conf.mycompany.com/conf/prod.conf
结帐更多关于:
https://www.playframework.com/documentation/2.3.x/ProductionConfiguration
我通常有一个基本配置 (application.conf
) 和每个环境的三个额外配置。在 Play Framework 2.4
中,可以通过扩展 GuiceApplicationLoader 并将基础 conf 与您的环境特定的 conf 合并来完成。您可以更进一步,为每个环境提供不同的 guice 模块。
Scala 版本:
class CustomApplicationLoader extends GuiceApplicationLoader {
override protected def builder(context: Context): GuiceApplicationBuilder = {
val builder = initialBuilder.in(context.environment).overrides(overrides(context): _*)
context.environment.mode match {
case Prod =>
// start mode
val prodConf = Configuration(ConfigFactory.load("prod.conf"))
builder.loadConfig(prodConf ++ context.initialConfiguration).bindings(new ProdModule());
case Dev =>
// run mode
val devConf = Configuration(ConfigFactory.load("dev.conf"))
builder.loadConfig(devConf ++ context.initialConfiguration).bindings(new DevModule());
case Test =>
// test mode
val testConf = Configuration(ConfigFactory.load("test.conf"))
builder.loadConfig(testConf ++ context.initialConfiguration).bindings(new TestModule());
}
}
}
Java版本:
public class CustomApplicationLoader extends GuiceApplicationLoader {
@Override
public GuiceApplicationBuilder builder(ApplicationLoader.Context context) {
final Environment environment = context.environment();
GuiceApplicationBuilder builder = initialBuilder.in(environment);
Configuration config = context.initialConfiguration();
if (environment.isTest()) {
config = merge("test.conf", config);
builder = builder.bindings(new TestModule());
} else if (environment.isDev()) {
config = merge("dev.conf", config);
builder = builder.bindings(new DevModule());
} else if (environment.isProd()) {
config = merge("prod.conf", config);
builder = builder.bindings(new DevModule());
} else {
throw new IllegalStateException("No such mode.");
}
return builder.in(environment).loadConfig(config);
}
private Configuration merge(String configName, Configuration currentConfig) {
return new Configuration(currentConfig.getWrappedConfiguration().$plus$plus(new play.api.Configuration(ConfigFactory.load(configName))));
}
}
不要忘记将 play.application.loader = "modules.CustomApplicationLoader"
添加到您的 application.conf
。
在 Play
的较低版本中,可以通过使用 GlobalSettings
class 和覆盖 onLoadConfig
来实现类似的功能。在 Play 2.4
中的思想 GlobalSettings
是堕落的。
如果您不喜欢将 TestModule
中的 test.conf
和 test mocks
包含到您的生产版本中,您可以 filter
使用 sbt 的文件。
这件事可以通过加载基于环境的配置文件来完成,这些配置文件可以通过 -Dmode=staging/dev/prod
提供,并且为了加载文件,我在 Global.java 中重写了 GlobalSettings 的 onLoadConfig
.
Java 片段-
@Override
public Configuration onLoadConfig(Configuration config, File file,ClassLoader classLoader) {
Configuration updatedConfig = config;
String mode = config.getString("mode");
if (StringUtils.isNotEmpty(mode)) {
try {
File modeFolder = FileUtils.getFile(file, "conf/" + mode);
if (modeFolder.exists()) {
play.api.Configuration modeConfig = config.getWrappedConfiguration();
IOFileFilter fileFilter = new WildcardFileFilter("*.conf");
Collection<File> fileList = FileUtils.listFiles(modeFolder, fileFilter, null);
for (File confFile : fileList) {
modeConfig = modeConfig
.$plus$plus(new play.api.Configuration(ConfigFactory.parseFile(confFile)));
}
updatedConfig = new Configuration(modeConfig);
}
} catch (Exception e) {
Logger.error("Exception while loading configuration for mode : " + mode, e);
}
} else {
Logger.error("Please provide mode in which play application has to start (Ex. play -Dmode=<mode>) ");
}
对于每种模式,创建一个文件夹(名称与环境相同)并在该文件夹中保存环境特定配置。
Play 可以在开发模式(通过 run
)、生产模式(通过 start
)或测试模式下启动。有没有办法根据启动模式提供不同的配置文件 (conf/application.conf
)?
您可以使用 play 提供的 3 种方式之一设置不同的配置文件:
1 - Using -Dconfig.resource
It will search for an alternative configuration file in the application classpath (you usually provide these alternative configuration files into your application conf/ directory before packaging). Play will look into conf/ so you don’t have to add conf/.
$ /path/to/bin/ -Dconfig.resource=prod.conf
2 - Using -Dconfig.file
You can also specify another local configuration file not packaged into the application artifacts:
$ /path/to/bin/ -Dconfig.file=/opt/conf/prod.conf
3 - Using -Dconfig.url
You can also specify a configuration file to be loaded from any URL:
$ /path/to/bin/ -Dconfig.url=http://conf.mycompany.com/conf/prod.conf
结帐更多关于:
https://www.playframework.com/documentation/2.3.x/ProductionConfiguration
我通常有一个基本配置 (application.conf
) 和每个环境的三个额外配置。在 Play Framework 2.4
中,可以通过扩展 GuiceApplicationLoader 并将基础 conf 与您的环境特定的 conf 合并来完成。您可以更进一步,为每个环境提供不同的 guice 模块。
Scala 版本:
class CustomApplicationLoader extends GuiceApplicationLoader {
override protected def builder(context: Context): GuiceApplicationBuilder = {
val builder = initialBuilder.in(context.environment).overrides(overrides(context): _*)
context.environment.mode match {
case Prod =>
// start mode
val prodConf = Configuration(ConfigFactory.load("prod.conf"))
builder.loadConfig(prodConf ++ context.initialConfiguration).bindings(new ProdModule());
case Dev =>
// run mode
val devConf = Configuration(ConfigFactory.load("dev.conf"))
builder.loadConfig(devConf ++ context.initialConfiguration).bindings(new DevModule());
case Test =>
// test mode
val testConf = Configuration(ConfigFactory.load("test.conf"))
builder.loadConfig(testConf ++ context.initialConfiguration).bindings(new TestModule());
}
}
}
Java版本:
public class CustomApplicationLoader extends GuiceApplicationLoader {
@Override
public GuiceApplicationBuilder builder(ApplicationLoader.Context context) {
final Environment environment = context.environment();
GuiceApplicationBuilder builder = initialBuilder.in(environment);
Configuration config = context.initialConfiguration();
if (environment.isTest()) {
config = merge("test.conf", config);
builder = builder.bindings(new TestModule());
} else if (environment.isDev()) {
config = merge("dev.conf", config);
builder = builder.bindings(new DevModule());
} else if (environment.isProd()) {
config = merge("prod.conf", config);
builder = builder.bindings(new DevModule());
} else {
throw new IllegalStateException("No such mode.");
}
return builder.in(environment).loadConfig(config);
}
private Configuration merge(String configName, Configuration currentConfig) {
return new Configuration(currentConfig.getWrappedConfiguration().$plus$plus(new play.api.Configuration(ConfigFactory.load(configName))));
}
}
不要忘记将 play.application.loader = "modules.CustomApplicationLoader"
添加到您的 application.conf
。
在 Play
的较低版本中,可以通过使用 GlobalSettings
class 和覆盖 onLoadConfig
来实现类似的功能。在 Play 2.4
中的思想 GlobalSettings
是堕落的。
如果您不喜欢将 TestModule
中的 test.conf
和 test mocks
包含到您的生产版本中,您可以 filter
使用 sbt 的文件。
这件事可以通过加载基于环境的配置文件来完成,这些配置文件可以通过 -Dmode=staging/dev/prod
提供,并且为了加载文件,我在 Global.java 中重写了 GlobalSettings 的 onLoadConfig
.
Java 片段-
@Override
public Configuration onLoadConfig(Configuration config, File file,ClassLoader classLoader) {
Configuration updatedConfig = config;
String mode = config.getString("mode");
if (StringUtils.isNotEmpty(mode)) {
try {
File modeFolder = FileUtils.getFile(file, "conf/" + mode);
if (modeFolder.exists()) {
play.api.Configuration modeConfig = config.getWrappedConfiguration();
IOFileFilter fileFilter = new WildcardFileFilter("*.conf");
Collection<File> fileList = FileUtils.listFiles(modeFolder, fileFilter, null);
for (File confFile : fileList) {
modeConfig = modeConfig
.$plus$plus(new play.api.Configuration(ConfigFactory.parseFile(confFile)));
}
updatedConfig = new Configuration(modeConfig);
}
} catch (Exception e) {
Logger.error("Exception while loading configuration for mode : " + mode, e);
}
} else {
Logger.error("Please provide mode in which play application has to start (Ex. play -Dmode=<mode>) ");
}
对于每种模式,创建一个文件夹(名称与环境相同)并在该文件夹中保存环境特定配置。