Tobias
Tobias

Reputation: 5463

Dagger2 - where define the scopes

I have a small question to Dagger2. But first let me show the sample code:

@Singleton
@Component(module={ApplicationModule.class})
public Interface ApplicationComponent {

}


@Module
public class ApplicationModule {
    @Provides
    public Context provideContext() {
        return context;   
    }
}

I know that the objects from the component now are "Singletons".. My question... Did that have any effect to the Module? Is the Module also Singleton?

Upvotes: 0

Views: 63

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81588

No, the Module will not be singleton unless you specify the scope for the @Provides annotated provider methods as well.

@Singleton
@Component(module={ApplicationModule.class})
public Interface ApplicationComponent {
    Context context;
}


@Module
public class ApplicationModule {
    @Provides //unscoped, every injection is new instance
    public Context context() {
        return context;   
    }

    @Provides
    @Singleton //scoped, one instance per component
    public Something something() {
        return new Something();
    }
}

Upvotes: 1

Related Questions