diff --git a/coverage-report/src/test/java/org/jooby/issues/Issue767.java b/coverage-report/src/test/java/org/jooby/issues/Issue767.java new file mode 100644 index 0000000000..ea1a43b041 --- /dev/null +++ b/coverage-report/src/test/java/org/jooby/issues/Issue767.java @@ -0,0 +1,41 @@ +package org.jooby.issues; + +import org.jooby.Jooby; +import org.jooby.test.ServerFeature; +import org.junit.Test; + +public class Issue767 extends ServerFeature { + + public static class Service { + public String doWork() { + return "OK"; + } + } + + public static class Foo extends Jooby { + { + get("/foo", () -> require(Service.class).doWork()); + } + } + + { + get("/767", () -> require(Service.class).doWork()); + + use("/767", new Foo()); + } + + @Test + public void shouldHaveAccessFromTopApp() throws Exception { + request() + .get("/767") + .expect("OK"); + } + + @Test + public void shouldHaveAccessFromSubApp() throws Exception { + request() + .get("/767/foo") + .expect("OK"); + } + +} diff --git a/jooby/src/main/java/org/jooby/Jooby.java b/jooby/src/main/java/org/jooby/Jooby.java index 434e05e2cb..78c0e9d072 100644 --- a/jooby/src/main/java/org/jooby/Jooby.java +++ b/jooby/src/main/java/org/jooby/Jooby.java @@ -698,6 +698,8 @@ public EnvDep(final Predicate predicate, final Consumer callback */ private transient BiFunction injectorFactory = Guice::createInjector; + private List apprefs; + /** * Creates a new {@link Jooby} application. */ @@ -780,6 +782,10 @@ private Jooby use(final Optional path, final Jooby app) { if (app.mapper != null) { this.map(app.mapper); } + if (apprefs == null) { + apprefs = new ArrayList<>(); + } + apprefs.add(app); return this; } @@ -2755,6 +2761,11 @@ private Injector bootstrap(final Config args, }; Injector injector = injectorFactory.apply(stage, joobyModule); + if (apprefs != null) { + apprefs.forEach(app -> app.injector = injector); + apprefs.clear(); + apprefs = null; + } onStart.addAll(0, finalEnv.startTasks()); onStarted.addAll(0, finalEnv.startedTasks());