Rubin Yoo
Rubin Yoo

Reputation: 2720

Is it possible to scope constructor injection in dagger 2?

I have a code something like this and I want to make it scoped. But I found that this is not working and it seems only possible through in a module. I wasn't able to find a proper question for this and is it possible to scope a constructor injection?

Does not work

@AppScope
@Inject
public StackOverflow() {
}

Scope works!!

@Module
public InternetModule {
   @AppScope
   @Provides
   public StackOverflow provideStackOverflow() {
      return new StackOverflow();
   }
}

Upvotes: 5

Views: 292

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81539

You need to put the scope on the class

@AppScope
public class Blah {
    @Inject StackOverFlow stackOverflow;

    @Inject 
    public Blah()  {
    }
}

Upvotes: 4

Related Questions