Skip to content

Commit 3518fe7

Browse files
committed
redo Guice integration as Jooby extension
1 parent 3b4ca12 commit 3518fe7

File tree

9 files changed

+168
-46
lines changed

9 files changed

+168
-46
lines changed

docs/dependency-injection.adoc

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -171,23 +171,24 @@ fun main(args: Array<String>) {
171171

172172
1) Add Guice dependency to your project:
173173

174-
[dependency, groupId="com.google.inject", artifactId="guice", version="4.2.2"]
174+
[dependency, artifactId="jooby-guice"]
175175
.
176176

177-
2) Bootstrap Guice from application:
177+
2) Install Guice:
178178

179-
.Java
179+
.Installing Guice
180180
[source, java, role = "primary"]
181181
----
182+
183+
import io.jooby.di.Guiceby;
184+
182185
public class App extends Jooby {
183186
184187
{
185-
Injector injector = Guice.createInjector(...); <1>
186-
187-
registry(injector); <2>
188+
install(new Guiceby()); <1>
188189
189190
get("/", ctx -> {
190-
MyService service = require(MyService.class); <3>
191+
MyService service = require(MyService.class); <2>
191192
return service.doSomething();
192193
});
193194
}
@@ -197,66 +198,62 @@ public class App extends Jooby {
197198
.Kotlin
198199
[source, kotlin, role = "secondary"]
199200
----
201+
import io.jooby.di.Guiceby
200202
import io.jooby.run
201203
202204
fun main(args: Array<String>) {
203205
run(args) {
204-
val injector = Guice.createInjector(...) <1>
206+
install(Guiceby()) <1>
205207
206-
registry(injector) <2>
207-
208208
get ("/") { ctx ->
209-
val service = require(MyService::class) <3>
209+
val service = require(MyService::class) <2>
210210
service.doSomething()
211211
}
212212
}
213213
}
214214
----
215215

216-
<1> Bootstrap Guice and creates an Injector
217-
<2> Integrates javadoc:Jooby[require, java.lang.Class] with Guice
218-
<3> Access to an instance provided by Guice
216+
<1> Install Guice extension
217+
<2> The javadoc:Jooby[require, java.lang.Class] call is now resolved by Guice
219218

220219
[id=guice-mvc-routes]
221220
==== MVC routes
222221

223-
Integration of MVC routes with Guice is as simple as:
222+
Guice will also provisioning MVC routes
224223

225-
226224
.MVC and Guice
227225
[source, java, role = "primary"]
228226
----
227+
228+
import io.jooby.di.Guiceby;
229+
229230
public class App extends Jooby {
230231
231232
{
232-
Injector injector = Guice.createInjector(...); <1>
233+
install(new Guiceby()) <1>
233234
234-
registry(injector); <2>
235-
236-
mvc(MyController.class); <3>
235+
mvc(MyController.class); <2>
237236
}
238237
}
239238
----
240239

241240
.Kotlin
242241
[source, kotlin, role = "secondary"]
243242
----
243+
import io.jooby.di.Guiceby
244244
import io.jooby.run
245245
246246
fun main(args: Array<String>) {
247247
run(args) {
248-
val injector = Guice.createInjector(...) <1>
248+
install(Guiceby()) <1>
249249
250-
registry(injector) <2>
251-
252-
mvc(MyController::class) <3>
250+
mvc(MyController::class) <2>
253251
}
254252
}
255253
----
256254

257-
<1> Bootstrap Guice and creates an Injector
258-
<2> Integrates javadoc:Jooby[require, java.lang.Class] with Guice
259-
<3> Let Guice creates and provision `MyController`
255+
<1> Install Guice extension
256+
<2> Register a MVC route
260257

261258
The lifecycle of `MyController` is now managed by Guice. Also:
262259

jooby/pom.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,6 @@
256256
<optional>true</optional>
257257
</dependency>
258258

259-
<!-- Guice -->
260-
<dependency>
261-
<groupId>com.google.inject</groupId>
262-
<artifactId>guice</artifactId>
263-
<optional>true</optional>
264-
</dependency>
265-
266259
<!-- weld -->
267260
<dependency>
268261
<groupId>org.jboss.weld.se</groupId>

jooby/src/main/java/io/jooby/Env.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
*/
1616
package io.jooby;
1717

18-
import com.google.common.base.Strings;
1918
import com.typesafe.config.Config;
2019
import com.typesafe.config.ConfigFactory;
2120
import com.typesafe.config.ConfigParseOptions;
22-
import org.slf4j.Logger;
2321

2422
import javax.annotation.Nonnull;
2523
import java.lang.management.ManagementFactory;

jooby/src/main/java/io/jooby/internal/RegistryImpl.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@
1616
package io.jooby.internal;
1717

1818
import io.jooby.Registry;
19-
import io.jooby.internal.registry.GuiceAdapter;
2019
import io.jooby.internal.registry.WeldAdapter;
2120

2221
public class RegistryImpl {
2322
public static Registry wrap(ClassLoader loader, Object candidate) {
2423
if (candidate instanceof Registry) {
2524
return (Registry) candidate;
2625
}
27-
if (isInstanceOf(loader, "com.google.inject.Injector", candidate)) {
28-
return new GuiceAdapter(candidate);
29-
}
3026
if (isInstanceOf(loader, "org.jboss.weld.environment.se.WeldContainer", candidate)) {
3127
return new WeldAdapter(candidate);
3228
}

modules/jooby-guice/pom.xml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
6+
<parent>
7+
<groupId>io.jooby</groupId>
8+
<artifactId>modules</artifactId>
9+
<version>2.0.0.M1</version>
10+
</parent>
11+
12+
<modelVersion>4.0.0</modelVersion>
13+
<artifactId>jooby-guice</artifactId>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>com.google.code.findbugs</groupId>
18+
<artifactId>jsr305</artifactId>
19+
<scope>provided</scope>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>io.jooby</groupId>
24+
<artifactId>jooby</artifactId>
25+
<version>${jooby.version}</version>
26+
</dependency>
27+
28+
<!-- Guice -->
29+
<dependency>
30+
<groupId>com.google.inject</groupId>
31+
<artifactId>guice</artifactId>
32+
</dependency>
33+
34+
<!-- Test dependencies -->
35+
<dependency>
36+
<groupId>org.junit.jupiter</groupId>
37+
<artifactId>junit-jupiter-engine</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.jacoco</groupId>
43+
<artifactId>org.jacoco.agent</artifactId>
44+
<classifier>runtime</classifier>
45+
<scope>test</scope>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>com.squareup.okhttp3</groupId>
50+
<artifactId>okhttp</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>io.jooby</groupId>
56+
<artifactId>jooby</artifactId>
57+
<version>${jooby.version}</version>
58+
<classifier>tests</classifier>
59+
<scope>test</scope>
60+
</dependency>
61+
</dependencies>
62+
63+
<build>
64+
<plugins>
65+
<plugin>
66+
<groupId>com.mycila</groupId>
67+
<artifactId>license-maven-plugin</artifactId>
68+
<version>${license-maven-plugin.version}</version>
69+
<configuration>
70+
<header>etc${file.separator}LICENSE</header>
71+
<strictCheck>true</strictCheck>
72+
<skipExistingHeaders>true</skipExistingHeaders>
73+
<includes>
74+
<include>src/main/java/**/*.java</include>
75+
<include>src/main/java/**/*.kt</include>
76+
</includes>
77+
</configuration>
78+
<executions>
79+
<execution>
80+
<phase>verify</phase>
81+
<goals>
82+
<goal>format</goal>
83+
</goals>
84+
</execution>
85+
</executions>
86+
</plugin>
87+
</plugins>
88+
</build>
89+
</project>

jooby/src/main/java/io/jooby/internal/registry/GuiceAdapter.java renamed to modules/jooby-guice/src/main/java/io/jooby/di/GuiceRegistry.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* Copyright 2014 Edgar Espina
1515
*/
16-
package io.jooby.internal.registry;
16+
package io.jooby.di;
1717

1818
import com.google.inject.Injector;
1919
import com.google.inject.Key;
@@ -22,11 +22,11 @@
2222

2323
import javax.annotation.Nonnull;
2424

25-
public class GuiceAdapter implements Registry {
26-
private final Injector injector;
25+
public class GuiceRegistry implements Registry {
26+
private Injector injector;
2727

28-
public GuiceAdapter(Object candidate) {
29-
this.injector = (Injector) candidate;
28+
public GuiceRegistry(Injector injector) {
29+
this.injector = injector;
3030
}
3131

3232
@Nonnull @Override public <T> T require(@Nonnull Class<T> type) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*
14+
* Copyright 2014 Edgar Espina
15+
*/
16+
package io.jooby.di;
17+
18+
import com.google.inject.Guice;
19+
import com.google.inject.Injector;
20+
import com.google.inject.Module;
21+
import com.google.inject.Stage;
22+
import io.jooby.Env;
23+
import io.jooby.Extension;
24+
import io.jooby.Jooby;
25+
26+
import javax.annotation.Nonnull;
27+
28+
public class Guiceby implements Extension {
29+
30+
private Injector injector;
31+
private Module[] modules;
32+
33+
public Guiceby(@Nonnull Injector injector) {
34+
this.injector = injector;
35+
}
36+
37+
public Guiceby(@Nonnull Module... modules) {
38+
this.modules = modules;
39+
}
40+
41+
@Override public void install(@Nonnull Jooby application) {
42+
if (injector == null) {
43+
Env env = application.environment();
44+
Stage stage = env.name().equals("dev") ? Stage.DEVELOPMENT : Stage.PRODUCTION;
45+
injector = Guice.createInjector(stage, modules);
46+
}
47+
application.registry(new GuiceRegistry(injector));
48+
}
49+
}

modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<module>jooby-jackson</module>
2020
<module>jooby-freemarker</module>
2121
<module>jooby-handlebars</module>
22+
<module>jooby-guice</module>
2223
<module>jooby-spring</module>
2324
</modules>
2425

tests/src/test/kotlin/io/jooby/App.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ data class SearchQuery(val q: String)
44

55
fun main(args: Array<String>) {
66
run(args) {
7-
println(basePackage())
87
get {
98
val q = ctx.query<SearchQuery>()
109
q.q

0 commit comments

Comments
 (0)