Rubin Yoo Rubin Yoo - 2 months ago 21
Java Question

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();
}
}

Answer

You need to put the scope on the class

@AppScope
public class Blah {
    @Inject StackOverFlow stackOverflow;

    @Inject 
    public Blah()  {
    }
}