I Successfully used Qualified field injection construct injection and method injection , i have expect from dagger 2.10 to inject dependency to Qualified method like following code:
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DaggerMainActivityComponent.create().inject(this);
}
@Named("firstName")
@Inject
void initFirstName(String firstName){
}
@Named("lastName")
@Inject
void initLastName(String lastName){
}
@Module public class UserModule {
@Named("firstName")
@Provides
String provideFirstUserName() {
return "Nasser";
}
@Named("lastName")
@Provides
String provideLastUserName() {
return "Khosravi";
}
}
@Component(modules = { UserModule.class})
public interface MainActivityComponent {
void inject(MainActivity mainActivity);
@Named("firstName")
String getFirstName();
@Named("lastName")
String getLastName();
}
}
but when i use this code i get :
java.lang.String cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
There is a lot of simple tutorial about dagger in web but all of them are same and i can't find any example about Qualified method injection .
Why I Want Method Injection?
I prefer method injection over field injection because it is :
- clear than field injection
- you can simply set breack point and debug value injected
- you can assign value injected to private field
- ....
My Question:
Is it possible Qualified method injection in dagger 2? or my expect of method injection is wrong?
If it'a possible,How i can achieve it?
thanks for any advice.