Is two-phase binding mandatory?
QubitPi opened this issue · comments
In Elide-Standalone's ResourceConfig, we saw the following dependency injection:
// Bind api docs to given endpoint
//This looks strange, but Jersey binds its Abstract binders first, and then later it binds 'external'
//binders (like this HK2 version). This allows breaking dependency injection into two phases.
//Everything bound in the first phase can be accessed in the second phase.
register(new org.glassfish.hk2.utilities.binding.AbstractBinder() {
@Override
protected void configure() {
Elide elide = injector.getService(Elide.class, "elide");
elide.doScans(); <--- We are interested in this line
...
}
});
Is it OK to move elide.doScans()
into ElideBinder? i.e.
@AllArgsConstructor
public class ElideBinder extends AbstractBinder {
...
// Bind elide instance for injection into endpoint
bind(elide).to(Elide.class).named("elide");
elide.doScans(); <-- How about moving it here?
// Bind additional elements
bind(elideSettings).to(ElideSettings.class);
bind(elideSettings.getDictionary()).to(EntityDictionary.class);
bind(elideSettings.getDataStore()).to(DataStore.class).named("elideDataStore");
}
We would like to do this because we are not sure about the purpose of "breaking dependency injection into two phases" as mentioned in the code comment above. Are we missing anything?
We have verified that putting
elide.doScans()
inElideBinder
actually worked
Thank you for your attention.
I don't fully recall, but I believe HK2 DI bindings may not work correctly if you do scans before the first phase completes and HK2 is registered. This means serdes and security checks won't be injected.
Thank you very much, @aklish !
When we put HK2 DI bindings in the 2nd phase exactly as Elide standalone did, everything worked on my machine, but the same code was not working on someone else's machine (it said injector.getService(Elide.class, "elide")
returns null
there). This was the reason we considered doing scans before 1st phase finished, because that way it worked everywhere
I will investigate this further (see the next comment below) and see if we can remove the null
problem and keep the scan in the 2nd phase. Yeah, we want to follow Elide example as much as possible.
Thanks for the help
Everything just worked. We've managed to put scanning in the 2nd phase. The short answer is we have a config bug in our code previously.