I have a question regarding Android Dagger 2 und the usage of @Inject
and @Provide
annotations. Given are the following two simplified examples:
public class A {
String msg;
public A(String msg){
this.msg = msg;
}
}
public class B {
public A a;
public B(A a){
this.a = a;
}
}
@Module
public class AModule {
@Provides
A providesA(){
return new A("blah");
}
@Provides
B ProvidesB(A a)
{
return new B(a);
}
}
The example is pretty straight forward, I have two methods in my AModule
with @Provides
annotations. Therefore, Dagger can create an object of B
using an instance of A
with the string blah
.
My second example looks like this:
public class A {
String msg;
public A(String msg){
this.msg = msg;
}
}
public class B {
public A a;
@Inject
public B(A a){
this.a = a;
}
}
@Module
public class AModule {
@Provides
A providesA(){
return new A("blah");
}
}
In this example, Dagger can create an instance of B
because an object A
can be created using AModule
. The instance of B
can be created because it's constructor uses the @Inject
annotation.
So my question is: What's the difference between those two examples? Both seem to have the same semantics. Does the generated code differ and are there any pitfalls? Or is it just a matter or personal taste or best practices?