Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2 - what is the purpose of a @Singleton annotation class

From the dagger 2 Documentation I noticed that you can have a @Singleton annotated class. What is the purpose of marking a class as @Singleton as I have tried to do this in my code but a singleton object is NOT produced. I'm not clear on what use marking my class with this annotation serves.

From the documentation please focus on the following statement:

The @Singleton annotation on an injectable class also serves as documentation. It reminds potential maintainers that this class may be shared by multiple threads.*

@Singleton class CoffeeMaker {     // ... } 

UPDATE: After reviewing froger_mcs answer I see that in Dagger 2 you can provide injections either by a module OR by a constructor injection. So the following class, although not in a module, can be injected:

@Singleton public class MyClass {     @Inject     public MyClass() {      } } 

In this version the constructor is injected for us and in an Android activity you would just do the following and it will get provided:

@Inject MyClass myClass; //then in onCreate actually inject(this) from your graph of course. 
like image 894
j2emanue Avatar asked Jun 28 '15 12:06

j2emanue


People also ask

What is inject annotation in dagger?

With the @Inject annotation on the constructor, we instruct Dagger that an object of this class can be injected into other objects. Dagger automatically calls this constructor, if an instance of this class is requested.

What does @inject do dagger?

Use @Inject to annotate the constructor that Dagger should use to create instances of a class. When a new instance is requested, Dagger will obtain the required parameters values and invoke this constructor. class Thermosiphon implements Pump { private final Heater heater; @Inject Thermosiphon(Heater heater) { this.

What is dagger 2 used for?

Dagger is arguably the most used Dependency Injection, or DI, framework for Android. Many Android projects use Dagger to simplify building and providing dependencies across the app. It gives you the ability to create specific scopes, modules, and components, where each forms a piece of a puzzle: The dependency graph.

What is @component in dagger?

Now Component in a Dagger works by creating a graph of all the dependencies in the project so that it can find out where it should get those dependencies when they are needed. In order to implement this, an interface needs to be created and should be annotated with @Component.


1 Answers

@Singleton (and any other scope annotation) makes your class a single instance in your dependencies graph (it means that this instance will be "singleton" as long as Component object exists).

In short - everytime you're injecting @Singleton annotated class (with @Inject annotation) it will be the same instance as long as you inject it from the same Component.

For more I'm referring my blog post about how @Singleton and other scopes annotations works in Dagger 2: http://frogermcs.github.io/dependency-injection-with-dagger-2-custom-scopes/

like image 54
froger_mcs Avatar answered Sep 18 '22 10:09

froger_mcs