Skip to content

Commit 1fb593c

Browse files
committed
Add Base DI setup, use guice as DI using hk2-guice bridge
1 parent 30d50d4 commit 1fb593c

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

app/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ dependencies {
3535
implementation "com.google.protobuf:protobuf-java:$protobufVersion"
3636

3737
implementation('org.glassfish.jersey.containers:jersey-container-servlet:3.1.0')
38+
implementation('org.glassfish.jersey.inject:jersey-hk2:3.1.0')
3839

3940
//jOOQ & Postgres impl deps
4041
implementation "org.jooq:jooq:$jooqVersion"
@@ -44,6 +45,10 @@ dependencies {
4445
jooqGenerator "org.postgresql:postgresql:$postgresVersion"
4546

4647
implementation "com.google.inject:guice:$guiceVersion"
48+
implementation "org.glassfish.hk2:guice-bridge:3.0.3"
49+
50+
compileOnly 'org.projectlombok:lombok:1.18.24'
51+
annotationProcessor 'org.projectlombok:lombok:1.18.24'
4752

4853
compileOnly 'org.projectlombok:lombok:1.18.24'
4954
annotationProcessor 'org.projectlombok:lombok:1.18.24'
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
package org.vss;
22

3+
import com.google.inject.Guice;
4+
import com.google.inject.Injector;
5+
import jakarta.inject.Inject;
36
import jakarta.ws.rs.ApplicationPath;
7+
import org.glassfish.hk2.api.ServiceLocator;
48
import org.glassfish.jersey.server.ResourceConfig;
9+
import org.jvnet.hk2.guice.bridge.api.GuiceBridge;
10+
import org.jvnet.hk2.guice.bridge.api.GuiceIntoHK2Bridge;
11+
import org.vss.guice.BaseModule;
512

613
@ApplicationPath("/")
714
public class VSSApplication extends ResourceConfig {
8-
public VSSApplication() {
15+
16+
@Inject
17+
public VSSApplication(ServiceLocator serviceLocator) {
18+
packages("org.vss");
19+
Injector injector = Guice.createInjector(new BaseModule());
20+
initGuiceIntoHK2Bridge(serviceLocator, injector);
21+
}
22+
23+
// By default, Jersey framework uses HK2 for dependency injection.
24+
// To use Guice as our dependency injection framework, we provide guice injector to hk2-bridge.
25+
// So that hk2 can query guice injector for creating/injecting objects.
26+
private void initGuiceIntoHK2Bridge(ServiceLocator serviceLocator, Injector injector) {
27+
GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
28+
GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
29+
guiceBridge.bridgeGuiceInjector(injector);
930
}
1031
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.vss.guice;
2+
3+
import com.google.inject.AbstractModule;
4+
import com.google.inject.Singleton;
5+
import org.vss.KVStore;
6+
import org.vss.impl.postgres.PostgresBackendImpl;
7+
8+
public class BaseModule extends AbstractModule {
9+
@Override
10+
protected void configure() {
11+
bind(KVStore.class).to(PostgresBackendImpl.class).in(Singleton.class);
12+
}
13+
}

0 commit comments

Comments
 (0)