【Dagger2】 案例大全 递归注解_2_在构造方法中构造对象时依赖另一个参数这个例子不要看,简直不能再乱了!public class Test { @Inject Person person;//6 private void test() { DaggerMainComponent.builder().mainModule(new MainModule()).build().inject(this);//1 System.out.println(person.name);//7 } public static void main(String[] args) { new Test().test();//包青天 } }@Component(modules = MainModule.class) interface MainComponent { void inject(Test obj); void injectName(Per
只有Inject是不可以的,必须有Component
public class Test {
@Inject Person person;
private void test() {
System.out.println(person.name);
}
public static void main(String[] args) {
new Test().test();//NullPointerException
}
}
class Person {
public String name;
@Inject
public Person() {
name = "默认的名字";
}
}
可以只有Inject和Component,没有Module
public class Test {
@Inject Person person;
private void test() {
DaggerMainComponent.builder().build().inject(this);//必须有注入的代码,否则根本无法将实例注入到目标类中
System.out.println(person.name);
}
public static void main(String[] args) {
new Test().test();//默认的名字
System.out.println(new Test().person.name);//NullPointerException。必须有注入的代码
}
}
@Component//可以不指定Module。也可以指定Module但不提供相应的方法
interface MainComponent {
void inject(Test obj);
}
class Person {
public String name;
@Inject
public Person() {
name = "默认的名字";
}
}
同时有Inject和Module时,优先使用Module
Component会首先从Module维度中查找类实例,若找到就用Module维度创建类实例,并停止查找Inject维度,否则才是从Inject维度查找类实例。
所以创建类实例级别:Module维度要高于Inject维度。
public class Test {
@Inject Person person;
private void test() {
DaggerMainComponent.builder().mainModule(new MainModule("白乾涛")).build().inject(this);
System.out.println(person.name);
}
public static void main(String[] args) {
new Test().test();//白乾涛
}
}
@Component(modules = MainModule.class)//指定Module
interface MainComponent {
void inject(Test obj);
}
@Module
class MainModule {
private String name;
public MainModule(String name) {
this.name = name;
}
@Provides
Person providerPerson() {
return new Person(name);//调用的是这里
}
}
class Person {
public String name;
@Inject
public Person() {
name = "默认的名字";//优先使用Module,所以不会调用这里
}
public Person(String name) {
this.name = name;
}
}
递归注解_1_在Module中构造对象时依赖另一个参数
public class Test {
@Inject Person person;//8
private void test() {
DaggerMainComponent.builder().mainModule(new MainModule()).build().inject(this);//1
System.out.println(person.name);//9
}
public static void main(String[] args) {
new Test().test();//包青天
}
}
@Component(modules = MainModule.class)
interface MainComponent {
void inject(Test obj);
void injectName(MainModule obj);
}
@Module
class MainModule {
@Inject String name;//5
@Provides
Person providerPerson() {//2
DaggerMainComponent.builder().mainModule(new MainModule()).build().injectName(this);//3
return new Person(name);//6
}
@Provides
String providerName() {//4
return "包青天";
}
}
//********************************************等价于下面这种形式,更简洁**********************************************
@Module
class MainModule {
@Provides
Person providerPerson(String name) {//2,5
return new Person(name);//6
}
@Provides
String providerName() {//3
System.out.println("即使后面用不到providerPerson方法中的形参name,也会走到这个方法里");
return "包青天";//4
}
}
class Person {
public String name;
@Inject
public Person() {
name = "默认的名字";//优先使用Module,所以不会调用这里
}
public Person(String name) {//7
this.name = name;
}
}