I'm creating a library that will be included as a jar, so it won't contain a main method. I'm wondering what is the best practice for bootstrapping Guice in this case. I have one top level singleton.
public class TestManager
{
private TestManager()
{
}
public static TestManager getInstance()
{
// construct and return singleton
}
public void createSomeObjects()
{
}
}
Where should I bootstrap Guice? I was thinking that in the constructor that I could call Guice.createInjector(new Module()); but it wouldn't inject any of the objects created in createSomeObjects().
Is there a common way to do this when you don't have a main method()?
Cheers.
Guice comes with a built-in @Singleton scope that reuses the same instance during the lifetime of an application within a single injector. Both javax. inject. Singleton and com.
Guice is an open source, Java-based dependency injection framework. It is quiet lightweight and is actively developed/managed by Google.
Guice solves the problem of having two different String objects you want to inject through "named annotations". You can bind these manually in your module, but there are libraries which will pull these from your configuration that make use of Names.
Guice forbids null by default So if something tries to supply null for an object, Guice will refuse to inject it and throw a NULL_INJECTED_INTO_NON_NULLABLE ProvisionException error instead. If null is permissible by your class, you can annotate the field or parameter with @Nullable .
Much like logging configurations, if this is a true library then your options are pretty much this:
Trying to make the library super-smart to do self-configuration often ends up with somewhat inflexible, hard to test class hierarchies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With