Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2 - how to create/provide a EagerSingleton

I am having trouble with the Dagger 2 dependency injection framework. I would like to create an EagerSingleton. I assume that dagger 2 creates lazy loaded singletons when I use the @Singleton annotation. How do I create EagerSingleton using the Dagger 2 framework ?

like image 801
j2emanue Avatar asked Jun 28 '15 14:06

j2emanue


People also ask

How do you add a dagger dependency?

In order to use dependency injection with the help of dagger 2 libraries, we need to add it's dependency. Go to Gradle Scripts > build. gradle(Module: app) and add the following dependencies. After adding these dependencies you need to click on Sync Now.

How does dagger generate code?

Dagger automatically generates code that mimics the code you would otherwise have hand-written. Because the code is generated at compile time, it's traceable and more performant than other reflection-based solutions such as Guice. Note: Use Hilt for dependency injection on Android.

What is @singleton in dagger?

The return type annotated with a @Provides annotation is used to associate this instantiation with any other modules of the same type. The @Singleton annotation is used to declare to Dagger that the provided object is to be only initialized only once during the entire lifecycle of the Component which uses that Module.

What is Dagger 2?

In this tutorial, we'll take a look at Dagger 2 – a fast and lightweight dependency injection framework. The framework is available for both Java and Android, but the high-performance derived from compile-time injection makes it a leading solution for the latter.

How do I use dagger 2 without component dependencies?

You can use the @Singleton annotation to indicate that there should be only one instance of the object. This is the most basic example of using Dagger 2 without component dependencies, subcomponents, complex modules, or custom scopes. The main Dagger 2 features used are:

How to create a new object every time with dagger?

For example, if you need to create a new object every time, the provider will look like this: A provider can contain any arbitrary code, including network (please don’t) and factory method calls. However, if the constructor is public and all parameters can be provided by Dagger, you can annotate the constructor with @Inject like this:

What is Dagger 2’s method roots?

Calling code like an application’s main method or an Android Application accesses that graph via a well-defined set of roots. In Dagger 2, that set is defined by an interface with methods that have no arguments and return the desired type.


1 Answers

It can be solved by using dagger multibindings. First, you need to create the interface:

public interface EagerInit {    
    void eagerInit();
}

In the EagerModule you bind EagerInit implementations to set, so you can access it in the EagerComponent:

@Module
public abstract class EagerModule {

    @Binds
    @IntoSet
    abstract EagerInit eagerInitImpl1(EagerInitImpl1 eagerInitImpl1);

    @Binds
    @IntoSet
    abstract EagerInit eagerInitImpl2(EagerInitImpl2 eagerInitImpl2);

}

@Component(modules = {EagerModule.class})
public interface EagerComponent {

    Set<EagerInit> getEagerInits();

}

After creating the EagerComponent you simply call:

component.getEagerInits().forEach(EagerInit::eagerInit);
like image 145
sylwano Avatar answered Oct 13 '22 06:10

sylwano