В Play's application.conf
у меня есть несколько конфигов для моего приложения.Например:
pew.server-script {
python {
exec = "python {1}"
filename = "{1}.py"
}
}
Во время миграции Play с 2.6.20 на 2.7.0 я обнаружил, что мои тесты не работают, потому что сценарий pew.server отсутствует в Config.Это пример теста:
private final Application application = prepareApplication();
static Application prepareApplication() {
Map<String, Object> additional = new HashMap<>(inMemoryDatabase("prototype"));
// skipped
additional.put("pew.scriptsFolder", "example");
// skipped
return new GuiceApplicationBuilder()
.overrides(Bindings.bind(HttpExecutionContext.class).to(
() -> new HttpExecutionContext(ForkJoinPool.commonPool())
))
.configure(ConfigFactory.parseMap(additional))
.build();
}
@Test
public void regAndLogin() {
running(application, () -> {
registerUser(application);
Http.RequestBuilder request = fakeRequest()
.method(GET)
.host("localhost")
.header("Authorization", basicKey)
.uri("/user/test");
Result result = route(application, request);
assertEquals(OK, result.status());
});
}
Сбой при инициализации модуля при вызове GuiceApplicationBuilder.build ():
bind(ConfigService.class).asEagerSingleton();
ConfigService выглядит следующим образом:
@Singleton
public class ConfigService {
private Config config;
// skipped
@Inject
public ConfigService(Config config) {
this.config = config;
decodeSettings();
}
@SuppressWarnings("unchecked")
private void decodeSettings() {
Map<String, Map<String, String>> cfg = (Map<String, Map<String, String>>) config.getAnyRef("pew.server-script");
// skipped
}
// skipped
}
За этим исключением:
com.google.inject.CreationException: Unable to create injector, see the following errors:
1) Error injecting constructor, com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'pew.server-script'
at services.ConfigService.<init>(ConfigService.java:17)
at Module.configure(Module.java:24) (via modules: com.google.inject.util.Modules$OverrideModule -> Module)
while locating services.ConfigService
1 error
at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:543)
at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:186)
at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:109)
at com.google.inject.Guice.createInjector(Guice.java:87)
at com.google.inject.Guice.createInjector(Guice.java:78)
at play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:186)
at play.inject.guice.GuiceBuilder.injector(GuiceBuilder.java:210)
at play.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.java:115)
at controller.ApplicationBuilder.prepareApplication(ApplicationBuilder.java:51)
at controller.ApplicationBuilder.prepareApplication(ApplicationBuilder.java:27)
at controller.UnitTests.<init>(UnitTests.java:38)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance( NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance( DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'pew.server-script'
at com.typesafe.config.impl.SimpleConfig.findKeyOrNull(SimpleConfig.java:156)
at com.typesafe.config.impl.SimpleConfig.findOrNull(SimpleConfig.java:174)
at com.typesafe.config.impl.SimpleConfig.findOrNull(SimpleConfig.java:180)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:188)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:193)
at com.typesafe.config.impl.SimpleConfig.getAnyRef(SimpleConfig.java:279)
at services.ConfigService.decodeSettings(ConfigService.java:24)
at services.ConfigService.<init>(ConfigService.java:19)
at services.ConfigService$$FastClassByGuice$$a3848b63.newInstance(<generated>)
at com.google.inject.internal.DefaultConstructionProxyFactory$FastClassProxy.newInstance( DefaultConstructionProxyFactory.java:89)
at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:114)
at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:91)
at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:306)
at com.google.inject.internal.ProviderToInternalFactoryAdapter.get( ProviderToInternalFactoryAdapter.java:40)
at com.google.inject.internal.SingletonScope$1.get(SingletonScope.java:168)
at com.google.inject.internal.InternalFactoryToProviderAdapter.get( InternalFactoryToProviderAdapter.java:39)
at com.google.inject.internal.InternalInjectorCreator.loadEagerSingletons( InternalInjectorCreator.java:211)
at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:182)
... 38 more
Также происходит сбой Akka во время создания маршрутизатора с настройками из config и JPA - необходимые настройки не включены в Config.Я проверил с отладкой IntelliJ IDEA - из "pew" в экземпляре Config есть только pew.scriptFolder = "example", который был установлен в дополнительной конфигурации (функция prepareApplication()
).Я также попробовал это:
.configure(ConfigFactory.parseFile(new File("conf/application.conf")).resolve())
Но это приводит к краху Config, ApplicationLifecycle, Environment, инъекция конфигурации.Как получить настройки ввода из application.conf в Application?