Advanced architecture, principle and Dagger2 of Detailed

table of Contents

  • A: Dagger2 what is?
  • Two: Why should Dagger2
  • Three: Dagger2 How to Use
  1. The basic concept
  2. How to use Dagger2
  3. Advanced Usage

(1) construction method requires any parameters when
dependency between (2) module
(3) @Named annotated with
(4) @Singleton annotations
(5) custom, Scoped
(. 6) Subcomponent
(. 7) and the lazy Provider

  • Four: MVP + Dagger2

Ps: the end the architects have advanced data and information face questions

A: Dagger2 what is?

Is a dependency injection framework, butterknife is a dependency injection framework. However butterknife, most called butter knife, Dagger2 called weapon ah, his main role is to manage an object, its purpose is to reduce the coupling procedure.

Two: Why should Dagger2

Advanced architecture, principle and Dagger2 of Detailed

Now I will hand the

public class A {
 public void eat() {
 System.out.print("吃饭了");
 }
}

We will use the time

A a = new A();
a.eat();

If now changed, the early A constructor must pass objects B

 public class A {
 private B b;
 public A(B b) {
 this.b = b;
 }
 public void eat() {
 System.out.print("吃饭了");
 }
}

So use the time

A a = new A(new B());
a.eat();

Some people might say, what not to add an object, here is a very simple example I cited, see the feeling is very simple, but in the actual development, if we changed this a constructor. Does that mean, the entire project are changed, accidentally, is BUG ah

Three: Dagger2 How to Use

1. Basic concepts

Up to you to say, how to play, certainly ignorant force, and here I would like to explain a few concepts, want to have a cognitive, looking down, will be much better, Dagger by @Inject use of a specific object that it is provided by the @Provides notes, but, um, @Provides only in a fixed module, which is @Module notes, we find, not go directly to the module, but went @Component

Advanced architecture, principle and Dagger2 of Detailed

We derive the reverse, when we use

@Inject
A a

想要获取a对象的示例的时候,Dagger2 会先去找,当前Activity或者Fragment所连接的桥梁,例如上图中,连接的只有一个桥梁,实际上可以有多个,这个桥梁,会去寻找他所依赖的模块,如图中,依赖了模块A,和模块B,然后在模块中,会去寻找@Providers注解,去寻找A的实例化对象。

2. 如何使用Dagger2

(1) 引入依赖库

Dagger2官网

compile 'com.google.dagger:dagger:2.11'
 annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
(2) 创建Moudule
//第一步 添加@Module 注解
@Module
public class MainModule {
}
(3)创建具体的示例
//第一步 添加@Module 注解
@Module
public class MainModule {
 //第二步 使用Provider 注解 实例化对象
 @Provides
 A providerA() {
 return new A();
 }
}
(4)创建一个Component
//第一步 添加@Component
//第二步 添加module
@Component(modules = {MainModule.class})
public interface MainComponent {
 //第三步 写一个方法 绑定Activity /Fragment
 void inject(MainActivity activity);
}
(5)Rebuild Project

Advanced architecture, principle and Dagger2 of Detailed

然后AS 会自动帮我们生成一个

Advanced architecture, principle and Dagger2 of Detailed

开头都是以Dagger开始的

(6)将Component与Activity/Fragment绑定关系
package com.allens.daggerdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.allens.daggerdemo.Bean.A;
import com.allens.daggerdemo.component.DaggerMainConponent;
import javax.inject.Inject;

public class MainActivity extends AppCompatActivity {
 /***
 * 第二步 使用Inject 注解,获取到A 对象的实例
 */
 @Inject
 A a;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 /***
 * 第一步 添加依赖关系
 */
 //第一种方式
 DaggerMainConponent.create().inject(this);

 //第二种方式
 DaggerMainConponent.builder().build().inject(this);

 /***
 * 第三步 调用A 对象的方法
 */
 a.eat();
 }
}

肯定有小伙伴说了,为了拿到一个对象,这么大个弯,太麻烦了。别急慢慢看,路要一步一步走嘛

3. 高级用法

(1)构造方法需要其他参数时候

怎么说呢,就和最来时的意思一样,

A a = new A(new B());
a.eat();

这种情况,如何使用Dagger2呢

肯定有小伙伴这么想

@Provides
 A providerA() {
 return new A(new B());
 }

直接 new 一个B ,这样的使用方法,是不对的!!!!!!,不对的!!!!!!!,不对的!!!!!!!!!

正确的打开方式

这时候,我们什么都不用该,只需要在moudule中添加一个依赖就可以了

@Module
public class MainModule {

 /***
 * 构造方法需要其他参数时候
 *
 * @return
 */
 @Provides
 B providerB() {
 return new B();
 }

 @Provides
 A providerA(B b) {
 return new A(b);
 }
}
(2) 模块之间的依赖关系

Advanced architecture, principle and Dagger2 of Detailed

模块与模块之间的联系,

@Module (includes = {BModule.class})// includes 引入)
public class AModule {
 @Provides
 A providerA() {
 return new A();
 }
}

这样的话,Dagger会现在A moudule 中寻找对象,如果没找到,会去找module B 中是否有被Inject注解的对象,如果还是没有,那么GG,抛出异常

一个Component 应用多个 module

@Component(modules = {AModule.class,BModule.class})
public interface MainComponent {
 void inject(MainActivity activity);
}

dependencies 依赖其他Component

@Component(modules = {MainModule.class}, dependencies = AppConponent.class)
public interface MainConponent {
 void inject(MainActivity activity);
}

注意 这里有坑。一下会讲解

(3) @Named注解使用

相当于有个表示,虽然大家都是同一个对象,但是实例化对象不同就不如

A a1 = new A();
A a2 = new A();

// a1 a2 能一样嘛

Module中 使用@Named注解

@Module
public class MainModule {

 private MainActivity activity;

 public MainModule(MainActivity activity) {
 this.activity = activity;
 }

 @Named("dev")
 @Provides
 MainApi provideMainApiDev(MainChildApi mainChildApi, String url) {
 return new MainApi(mainChildApi, activity,"dev");
 }

 @Named("release")
 @Provides
 MainApi provideMainApiRelease(MainChildApi mainChildApi, String url) {
 return new MainApi(mainChildApi, activity,"release");
 }

}

在Activity/Fragment中使用

public class MainActivity extends AppCompatActivity {

 @Named("dev")
 @Inject
 MainApi apiDev;

 @Named("release")
 @Inject
 MainApi apiRelease;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 DaggerMainComponent.builder()
 .mainModule(new MainModule(this))
 .mainChildModule(new MainChildModule())
 .build()
 .inject(this);
 apiDev.eat();
 apiRelease.eat();
 Log.i("TAG","apiDev--->" + apiDev);
 Log.i("TAG","apiRelease--->" + apiRelease);
 }

}

打印Log

07-14 01:46:01.170 2006-2006/? I/TAG: apiDev--->com.allen.rxjava.MainApi@477928f
07-14 01:46:01.170 2006-2006/? I/TAG: apiRelease--->com.allen.rxjava.MainApi@f2b291c
(4) @Singleton注解

单利模式,是不是超级方便,你想然哪个对象单利化,直接在他的Provider上添加@Singleton 就行了

例如

@Singleton
 @Provides
 A providerA(B b) {
 return new A(b);
 }

注意: 第一个坑!!!
如果 moudule所依赖的Comonent 中有被单利的对象,那么Conponnent也必须是单利的

@Singleton
@Component(modules = {MainModule.class})
public interface MainConponent {
}

然后 在Activity中使用,直接打印a1 a2 的地址,

@Inject
 A a2;
 @Inject
 A a1;

可以看到Log

12-30 01:32:58.420 3987-3987/com.allens.daggerdemo E/TAG: A1---->com.allens.daggerdemo.Bean.A@11fa1ba
12-30 01:32:58.420 3987-3987/com.allens.daggerdemo E/TAG: A1---->com.allens.daggerdemo.Bean.A@11fa1ba

不相信的小伙伴可以吧@Singleton去掉试试

现在我们完成了单利,然后做了一个事情,就是点击某个按钮,跳转到一个新的Activiry,两边都引用同样一个A 对象,打印A 的地址,

说一下,一个Conponent 可以被对个Activity/Fragment 引用,如

@Singleton
@Component(modules = {MainModule.class})
public interface MainConponent {
 void inject(MainActivity activity);
 void inject(TestAct activity);
}

上面与两个Activity, MainActivity 和 TestAct ,都引用相同的 对象,答应地址看看

12-30 00:48:17.477 2788-2788/com.allens.daggerdemo E/TAG: A1---->com.allens.daggerdemo.Bean.A@11fa1ba
12-30 00:48:17.517 2788-2788/com.allens.daggerdemo E/TAG: A2---->com.allens.daggerdemo.Bean.A@4f81861

竟然不同,说好的单利呢

注意: 第二个坑,单利对象只能在同一个Activity中有效。不同的Activity 持有的对象不同

那有人就要问了,没什么办法么,我就想全局只要一个实例化对象啊? 办法肯定是有的,

(5) 自定义Scoped
/**
 * @作者 : Android架构
 * @创建日期 :2017/7/14 下午3:04
 * @方法作用:
 * 参考Singleton 的写法
 * Scope 标注是Scope
 * Documented 标记在文档
 * @Retention(RUNTIME) 运行时级别
 */
@Scope
@Documented
@Retention(RUNTIME)
public @interface ActivityScoped {
}

首先想一下,什么样的对象,能够做到全局单例,生命周期肯定和APP 绑定嘛,这里我做演示,一个AppAip 我们要对这个对象,全局单利,所以二话不说,先给Application 来个全家桶,

Module

@Module
public class AppModule {

 @Singleton
 @Provides
 AppApi providerAppApi() {
 return new AppApi();
 }
}

Component

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
 AppApi getAppApi();
}

Application

public class MyApp extends Application {

 private AppConponent appComponent;

 @Override
 public void onCreate() {
 super.onCreate();
 appComponent = DaggerAppConpoment.create();
 }

 public AppConponent getAppComponent() {
 return appConponent;
 }
}

最后是如何使用

首先,这个是个桥梁,依赖方式,上文已经说过了

@ActivityScoped
@Component(modules = {MainModule.class}, dependencies = AppConponent.class)
public interface MainComponent {
 void inject(MainActivity activity);
 void inject(TestAct activity);
}

细心的小伙伴可能已经发现了,只有在上面一个MainComponent添加了一个@ActivityScoped,这里说明一下,@Singleton是Application 的单利

注意,第三个坑,子类component 依赖父类的component ,子类component的Scoped 要小于父类的Scoped,Singleton的级别是Application

所以,我们这里的@Singleton 级别大于我们自定义的@ActivityScoped,同时,对应module 所依赖的component ,也要放上相应的Scope

好吧,上面的例子,打印Log.

12-30 02:16:30.899 4717-4717/? E/TAG: A1---->com.allens.daggerdemo.Bean.AppApi@70bfc2
12-30 02:16:31.009 4717-4717/? E/TAG: A2---->com.allens.daggerdemo.Bean.AppApi@70bfc2

一样啦

爬坑指南(极度重要)

  1. Provide 如果是单例模式 对应的Compnent 也要是单例模式
  2. inject(Activity act) 不能放父类
  3. 即使使用了单利模式,在不同的Activity 对象还是不一样的
  4. 依赖component, component之间的Scoped 不能相同
  5. 子类component 依赖父类的component ,子类component的Scoped 要小于父类的Scoped,Singleton的级别是Application
  6. 多个Moudle 之间不能提供相同的对象实例
  7. Moudle 中使用了自定义的Scoped 那么对应的Compnent 使用同样的Scoped
(6)Subcomponent

这个是系统提供的一个Component,当使用Subcomponent,那么默认会依赖Component

例如

@Subcomponent(modules = TestSubModule.class)
public interface TestSubComponent {
 void inject(MainActivity activity);
}
@Component(modules = {MainModule.class})
public interface MainConponent {
 TestSubComponent add(TestSubModule module);
}

在TestSubComponent中 我void inject(MainActivity activity);,便是这个桥梁,我是要注入到MainActivity,但是dagger 并不会给我生成一个Dagger开头的DaggerTestSubComponent 这个类,如果我想使用TestSubModule.class里面提供的对象,依然还是使用DaggerMainConponent例如

DaggerMainConponent
 .builder()
 .mainModule(new MainModule())
 .build()
 .add(new TestSubModule())
 .inject(this);

可以看到这里有一个add的方法,真是我在MainConponent添加的TestSubComponent add(TestSubModule module);

(7)lazy 和 Provider
public class Main3Activity extends AppCompatActivity {

 @PresentForContext
 @Inject
 Lazy<Present> lazy;
 @PresentForName
 @Inject
 Provider<Present> provider;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main3);

 AppComponent appComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build();
 ActivityComponent activityComponent = DaggerActivityComponent.builder()
 .appComponent(appComponent)
 .activityModule(new ActivityModule())
 .build();

 activityComponent.injectActivity(this);
 Present present = lazy.get();
 Present present1 = provider.get();
 }
}

其中Lazy(懒加载)的作用好比component初始化了一个present对象,然后放到一个池子里,需要的时候就get它,所以你每次get的时候拿到的对象都是同一个;并且当你第一次去get时,它才会去初始化这个实例.

procider(强制加载)的作用:

1:同上当你第一次去get时,它才会去初始化这个实例

2:后面当你去get这个实例时,是否为同一个,取决于他Module里实现的方式

Four: MVP + Dagger2

This design architecture is now mainstream

MVP, I'll be in the back of this article describes, do not do too much to explain here

When you understand the MVP, you know, all in all the business logic Presenter,

In other words, presenter hold the objects, controls the entire logic of your program, in which the dagger, we just speak plainly to all presetner control objects on it

Attached below the directory structure, of course, merely as a reference. dagger powerful use or need your own experience, the following items are written when I just learned dagger of a project
Advanced architecture, principle and Dagger2 of Detailed

Advanced architecture, principle and Dagger2 of Detailed

You can see, I am all the activity or fragment Add All in the same Component, of course, if not now recommended, for example Utils you can specialize in a Component,

First I put the Module, the company projects a lot of things did not dare to impress, understand, I can see here a SplashPresenter, is the start page of Presneter, business logic

@Module
public class ApiModule {

 public ApiModule() {

 }

 @Provides
 @Singleton
 Handler provideHandler() {
 return new Handler();
 }

 @Provides
 @Singleton
 SQLiteDatabase provideSQLiteDatabase() {
 return new DataBaseHelper(MyApp.context, Config.SqlName, null, Config.SqlVersion).getWritableDatabase();
 }

 /**
 * @ User : Android架构
 * @ 创建日期 : 2017/7/13 下午3:24
 * @模块作用 :
 * <p>
 * ====================================================================================================================================
 * ====================================================================================================================================
 */
 private SplashPresenter splashPresenter;

 public ApiModule(SplashAct splashAct) {
 splashPresenter = new SplashPresenter(splashAct, new SplashModel());
 }

 @Provides
 @Singleton
 SplashPresenter provideSplashPresenter() {
 return splashPresenter;
 }

 .....
}

When I use can only be injected, the following code

public class SplashAct extends BaseActivity implements SplashContract.View {

 @Inject
 SplashPresenter presenter;

 @Inject
 Handler handler;

 @Inject
 ApiService apiService;

 @Override
 protected void onCreate() {
 setContentView(R.layout.activity_splash);
 }

 @Override
 protected void initInject() {
 DaggerApiComponent.builder()
 .apiModule(new ApiModule(this))
 .build()
 .inject(this);
 }

 @Override
 protected void initListener() {
 presenter.getWordsInfo(true, apiService);
 }

 @Override
 public void gotoLogInAct() {
 handler.postDelayed(new Runnable() {
 @Override
 public void run() {
 startActivity(new Intent(SplashAct.this, LogInAct.class));
 finish();
 }
 }, 1500);
 }
}

Guess you like

Origin blog.51cto.com/14332859/2429542