1. 在 gradle 中添加依赖
在主项目的 build.gradle 中添加 Dragger2 库的依赖
dependencies {
compile 'com.google.dagger:dagger:2.16'
annotationProcessor 'com.google.dagger:dagger-compiler:2.16'
}
2. Dragger2 最简单用法, 手工赋值
Demo 中只有两个简单的类 SimpleActivity 和 UserServer, SimpleActivity 有个 UserServer 类型的 server 变量, 通过添加 @Inject 注解为其赋值:
public class SimpleActivity extends AppCompatActivity {
@Inject
UserServer server;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
public class UserServer {
@Inject
public UserServer() {
}
}
Android Studio 编译后 Dragger2 库在 DraggerTest/app/build/generated/source/apt/debug 路径下生成了两个辅助类
import dagger.MembersInjector;
import javax.inject.Provider;
public final class SimpleActivity_MembersInjector implements MembersInjector<SimpleActivity> {
private final Provider<UserServer> serverProvider;
public SimpleActivity_MembersInjector(Provider<UserServer> serverProvider) {
this.serverProvider = serverProvider;
}
public static MembersInjector<SimpleActivity> create(Provider<UserServer> serverProvider) {
return new SimpleActivity_MembersInjector(serverProvider);
}
@Override
public void injectMembers(SimpleActivity instance) {
injectServer(instance, serverProvider.get());
}
public static void injectServer(SimpleActivity instance, UserServer server) {
instance.server = server;
}
}
import dagger.internal.Factory;
public final class UserServer_Factory implements Factory<UserServer> {
private static final UserServer_Factory INSTANCE = new UserServer_Factory();
@Override
public UserServer get() {
return provideInstance();
}
public static UserServer provideInstance() {
return new UserServer();
}
public static UserServer_Factory create() {
return INSTANCE;
}
public static UserServer newUserServer() {
return new UserServer();
}
}
SimpleActivity_MembersInjector 类中的 injectMembers 方法实现了为 server 变量赋值. UserServer_Factory 类正如其名, 是 UserServer 的一个工厂类, 它实现了 Factory 接口, 查看源码发现 Factory 接口是一个空接口, 它继承了 Provider 接口:
public interface Factory<T> extends Provider<T> {
}
public interface Provider<T> {
T get();
}
然后在 SimpleActivity 类中就可以调用自动生成的两个类去给 UserServer 变量赋值:
public class SimpleActivity extends AppCompatActivity {
@Inject
UserServer server;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Provider<UserServer> serverProvider = UserServer_Factory.create();
SimpleActivity_MembersInjector.create(serverProvider).injectMembers(this);
}
}
由于写的 UserServer 类很简单, 只有一个无参的构造方法, 所以自动生成的 UserServer_Factory 类也很简单, 给 UserServer 类赋值也很简单.
下面还有更简单的方法.
3. 引入 @Component 注解
我们再定义一个接口, 接口实现很简单, 重点是在类定义上的@Component注解
import dagger.Component;
@Component
public interface SimpleActivityComponent {
void inject(SimpleActivity activity);
}
编译后依然在 DraggerTest/app/build/generated/source/apt/debug 目录下会再生成一个辅助类:
public final class DaggerSimpleActivityComponent implements SimpleActivityComponent {
private DaggerSimpleActivityComponent(Builder builder) {}
public static Builder builder() {
return new Builder();
}
public static SimpleActivityComponent create() {
return new Builder().build();
}
@Override
public void inject(SimpleActivity activity) {
injectSimpleActivity(activity);
}
private SimpleActivity injectSimpleActivity(SimpleActivity instance) {
SimpleActivity_MembersInjector.injectServer(instance, new UserServer());
return instance;
}
public static final class Builder {
private Builder() {}
public SimpleActivityComponent build() {
return new DaggerSimpleActivityComponent(this);
}
}
}
DaggerSimpleActivityComponent 的 injectSimpleActivity 方法调用了 SimpleActivity_MembersInjector 类的 injectServer 方法, 直接 new 了一个 UserServer 对象, 没有用到前面提到的 UserServer_Factory 类.
这时在 SimpleActivity 类中再给 UserServer 变量赋值就可以改成如下:
public class SimpleActivity extends AppCompatActivity {
@Inject
UserServer server;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 手工注入
Provider<UserServer> serverProvider = UserServer_Factory.create();
SimpleActivity_MembersInjector.create(serverProvider).injectMembers(this);
// 通过 @Component 注解生成 DaggerSimpleActivityComponent 类来注入
DaggerSimpleActivityComponent.create().inject(this);
}
}
如果 SimpleActivity 中有多个变量需要注入, 通过这种方法也只需要一行, 不用和各个需要注入的类打交道.
网友评论