Dagger2初探(一)

      Dagger2是Android中依赖注入的一个类库,主要就是用来实现自动实例化的,也就是说我们不需要通过new这个关键字来实例化某个对象。而是通过Dagger2的依赖注入方式来实现.它的好处就是降低程序的耦合度。比如如果A的构造函数发生了改变,而B的构造中又使用到了A,那么B也需要修改。这只是两个,假如有更多的地方使用到了A,那么修改起来就会很繁琐。因此Dagger2正好能够带给我们便利。

那么首先,我们就来配置我们的Dagger2吧。

Dagger2中的Android Studio gradle配置:

implementation 'com.google.dagger:dagger:2.14.1'
annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1'

配置完成之后,Sync now一下,这样我们就可以开始使用Dagger2的各种注解了。

依赖注入的第一种:@Inject注解类的构造函数

首先,新建一个Student类:

public class Student {
	private String name;
	private int score;

	@Inject
	public Student(){
		this.name = "chendsir";
		this.score = 95;
	}

	public Student(String name, int score) {
		this.name = name;
		this.score = score;
	}

	@Override
	public String toString() {
		return "Student{" +
				"name='" + name + '\'' +
				", score=" + score +
				'}';

	}
}

我们可以看到在Student的无参构造函数中,有一个@Inject的注解,这是实现依赖注入的第一种方式,然后我们创建一个StudentComponent的接口:

@Component
public interface StudentComponent {
	void inject(MainActivity activity);
}

Component是将Student这个类与需要Student的实例化对象的MainActivity相关联起来。则MainActivity中的代码如下:

public class MainActivity extends AppCompatActivity {

	@Inject
	Student su;
	TextView tv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv = findViewById(R.id.id_tv_content);
		//DaggerStudentComponent需要在Rebuild之后才能识别,它是Dagger2自动生成的类
		DaggerStudentComponent.builder().build().inject(this);
		getStudentInfo();
	}

	public void getStudentInfo() {
		tv.setText(su.toString());
	}
}

其中有一个DaggerStudentCompoent是需要Rebuild一下项目,Dagger2自动帮忙生成的类,不然无法识别。同时我们也发现了在依赖需求方MainActivity中,它的成员变量Student上面有一个@Inject注解,同时Student类的构造函数上面也有一个@Inject注解,从这里我们可以看到@Inject不但可以作为依赖需求方,也可以作为依赖提供方。

那么运行一下,我们可以看到,APP上面的运行结果如图所示:

依赖注入的第二种实现:

  使用@Provides+@Module方式提供依赖实例

实际上第二种才算Dagger2的常用用法,那么我们先创建一个类StudnetTwo:

public class StudentTwo {
	private String name;
	private int score;

	public StudentTwo(String name, int score) {
		this.name = name;
		this.score = score;
	}

	@Override
	public String toString() {
		return "StudentTwo{" +
				"name='" + name + '\'' +
				", score=" + score +
				'}';

	}
}

然后再创建一个类StudentTwoModule:

@Module
public class StudentTwoModule {

		@Provides
		public String providerName() {
			return "chendiyang";
		}

		@Provides
		public int providerScore() {
			return 98;
		}

		@Provides
		public StudentTwo provideStudentTwo(String name,int score) {
			return new StudentTwo(name,score);
		}
}

可以看到在这个类的前面有一个@Module注解,表面这个类就是用来做实例化的,@Provides注解就是来提供不同的数据的。与第一种方式一样,我们也需要建立一个StudentTwoCompoent这个接口,它的作用就是将依赖提供方和依赖接收方建立联系,也可以称为依赖注入容器。

@Component(modules = StudentTwoModule.class) //表面数据源由StduentTwoModule提供
public interface StudentTwoComponent { //Component一个是接口,相当于中间件

	void inject(ThirdActivity activity); //这个是数据注入的地方,可以是Activity、Fragment、Java对象
}

最后,看看ThirdActivity中的代码:

public class ThirdActivity extends AppCompatActivity {

	@Inject
	StudentTwo studenttwo;

	TextView textView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_third);
		textView = findViewById(R.id.tv_content_three);
		DaggerStudentTwoComponent.builder().build().inject(this); //DaggerStudentTwoComponent是Dagger框架自动生成的
		getStudentInfo();
	}
	public void getStudentInfo() {
		textView.setText(studenttwo.toString());
	}
}

在使用DaggerStudentTwoComponent之前,要记得Rebuild一下,它是Dagger2自动生成的类。最后的运行结果,如图:

猜你喜欢

转载自blog.csdn.net/Chen_dSir/article/details/102455370
今日推荐