添加依赖
implementation 'com.google.dagger:dagger:2.4'
annotationProcessor 'com.google.dagger:dagger-compiler:2.4'
使用
在需要实例化的类中,构造无参构造方法,注解@Inject
public class Student {
private String s ;
@Inject
public Student(String s1){
this.s =s1;
}
public void run(){
Log.e("Student","log.run:"+s);
}
}
构造Component接口,有inject方法,需要注入的类
@dagger.Component
public interface Component {
void inject (LoginActivity activity);
}
必须先rebuild project才会生成DaggerComponent类,要使用实例的类
DaggerComponent.builder().build().inject(this);
student.run();