The Guice/HK2 Bridge
The Guice/HK2 Bridge can be used to inject Guice services
into HK2 services or inject HK2 services into Guice services.
It can be used bi-directionally as well.
Definitions
- A Guice service is a service that is instantiated (created) by Guice
- An HK2 service is a service that is instantiated (created) by HK2
Injecting Guice services into HK2 services
Guice services can be injected into any injection point in HK2.
In order to do this you must tell HK2 about the Guice Injector
which has the Guice service definitions.
This is accomplished in two steps.
In the first step you should have the ServiceLocator
that contains services you wish to be injected with Guice services.
You must initialize this ServiceLocator with some required
Guice/HK2 bridge services. You can do this using the utility class GuiceBridge.
This is a code snippet that initializes a ServiceLocator:
GuiceBridge.getGuiceBridge().initializeGuiceBridge(aServiceLocator);
In the second step you must tell your initialized ServiceLocator about the
specific Guice Injector(s) that you want it to look for services in.
You do this with the GuiceIntoHK2Bridge service that was added in the previous step.
The following code snippet adds a Guice Injector to be searched for services when injecting into HK2 services:
public void tieInjectorToLocator(ServiceLocator aServiceLocator, Injector guiceInjector) {
GuiceIntoHK2Bridge guiceBridge = aServiceLocator.getService(GuiceIntoHK2Bridge.class);
guiceBridge.bridgeGuiceInjector(guiceInjector);
}
Any Guice Injector added with the bridgeGuiceInjector
method will be searched for services that HK2 cannot otherwise find.
For example, if you have a service called GuiceService that is created by Guice, you can inject it into an HK2 service
(called HK2Service) like this:
@Service
public class HK2Service {
@Inject
private GuiceService guiceService;
}
Injecting HK2 services into Guice services
HK2 services can be injected into Guice services.
In order to do so, you must use the HK2Inject injection annotation.
For example, if you have an HK2 service named HK2Service that is to be injected
into a Guice service (called GuiceService) your code would look like this:
public class GuiceService {
@HK2Inject
private HK2Service hk2Service;
}
In order to do this we have provided an implementation of Module
that should be given to Guice when creating the Guice Injector.
This implementation of Module is [HK2IntoGuiceBridge][hk2intoguicebridge].
The following code snippet is an example of how you would create a Guice
Injector using the [HK2IntoGuiceBridge]hk2intoguicebridgemodule to
tell the Guice Injector about the ServiceLocator to use for finding HK2 services:
Injector injector = Guice.createInjector(
new HK2IntoGuiceBridge(serviceLocator),
// application modules);
Any Guice service that can be created with this Injector
will now search the provided ServiceLocator
when it encounters a service that is injected with the HK2Inject annotation.
Bi-Directional HK2 Guice Bridge
Guice and HK2 can bridge back and forth between each other.
The following code example shows how you could accomplish such a thing:
public Injector createBiDirectionalGuiceBridge(ServiceLocator serviceLocator,
Module... applicationModules) {
Module allModules[] = new Module[applicationModules.length + 1];
allModules[0] = new HK2IntoGuiceBridge(serviceLocator);
for (int lcv = 0; lcv < applicationModules.length; lcv++) {
allModules[lcv + 1] = applicationModules[lcv];
}
Injector injector = Guice.createInjector(allModules);
GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
GuiceIntoHK2Bridge g2h = serviceLocator.getService(GuiceIntoHK2Bridge.class);
g2h.bridgeGuiceInjector(injector);
return injector;
}
The above method will create a Guice Injector
where services created by Guice can be injected with HK2 services (using the HK2Inject annotation).
Also services created by HK2 can be injected with Guice services (using any supported HK2 injection annotation).