├── Dagger2Demo
├── app
│ ├── .gitignore
│ ├── src
│ │ ├── main
│ │ │ ├── res
│ │ │ │ ├── values
│ │ │ │ │ ├── strings.xml
│ │ │ │ │ ├── colors.xml
│ │ │ │ │ ├── dimens.xml
│ │ │ │ │ └── styles.xml
│ │ │ │ ├── mipmap-hdpi
│ │ │ │ │ └── ic_launcher.png
│ │ │ │ ├── mipmap-mdpi
│ │ │ │ │ └── ic_launcher.png
│ │ │ │ ├── mipmap-xhdpi
│ │ │ │ │ └── ic_launcher.png
│ │ │ │ ├── mipmap-xxhdpi
│ │ │ │ │ └── ic_launcher.png
│ │ │ │ ├── mipmap-xxxhdpi
│ │ │ │ │ └── ic_launcher.png
│ │ │ │ ├── values-w820dp
│ │ │ │ │ └── dimens.xml
│ │ │ │ └── layout
│ │ │ │ │ ├── activity_main.xml
│ │ │ │ │ ├── activity_a.xml
│ │ │ │ │ ├── activity_b.xml
│ │ │ │ │ ├── activity_c.xml
│ │ │ │ │ └── activity_login.xml
│ │ │ ├── java
│ │ │ │ └── com
│ │ │ │ │ └── iiseeuu
│ │ │ │ │ └── dagger2demo
│ │ │ │ │ ├── scope
│ │ │ │ │ ├── b
│ │ │ │ │ │ ├── BModule.java
│ │ │ │ │ │ ├── BComponent.java
│ │ │ │ │ │ └── BActivity.java
│ │ │ │ │ ├── c
│ │ │ │ │ │ ├── CModule.java
│ │ │ │ │ │ ├── CComponent.java
│ │ │ │ │ │ └── CActivity.java
│ │ │ │ │ ├── a
│ │ │ │ │ │ ├── AModule.java
│ │ │ │ │ │ ├── AComponent.java
│ │ │ │ │ │ └── AActivity.java
│ │ │ │ │ ├── AppComponent.java
│ │ │ │ │ ├── UserScope.java
│ │ │ │ │ ├── UserNamed.java
│ │ │ │ │ ├── User.java
│ │ │ │ │ ├── AppModule.java
│ │ │ │ │ ├── UserModule.java
│ │ │ │ │ ├── UserComponent.java
│ │ │ │ │ └── App.java
│ │ │ │ │ ├── AComponent.java
│ │ │ │ │ ├── AModule.java
│ │ │ │ │ ├── GsonModule.java
│ │ │ │ │ ├── A.java
│ │ │ │ │ ├── MainActivity2.java
│ │ │ │ │ └── MainActivity.java
│ │ │ └── AndroidManifest.xml
│ │ ├── test
│ │ │ └── java
│ │ │ │ └── com
│ │ │ │ └── iiseeuu
│ │ │ │ └── dagger2demo
│ │ │ │ └── ExampleUnitTest.java
│ │ └── androidTest
│ │ │ └── java
│ │ │ └── com
│ │ │ └── iiseeuu
│ │ │ └── dagger2demo
│ │ │ └── ExampleInstrumentationTest.java
│ ├── proguard-rules.pro
│ └── build.gradle
├── settings.gradle
├── .gitignore
└── build.gradle
├── untitled.jpg
├── device-2016-06-23-144101.png
├── device-2016-06-23-144137.png
├── device-2016-06-23-144149.png
└── README.md
/Dagger2Demo/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/Dagger2Demo/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/untitled.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wlj32011/dagger2demo/HEAD/untitled.jpg
--------------------------------------------------------------------------------
/device-2016-06-23-144101.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wlj32011/dagger2demo/HEAD/device-2016-06-23-144101.png
--------------------------------------------------------------------------------
/device-2016-06-23-144137.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wlj32011/dagger2demo/HEAD/device-2016-06-23-144137.png
--------------------------------------------------------------------------------
/device-2016-06-23-144149.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wlj32011/dagger2demo/HEAD/device-2016-06-23-144149.png
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Dagger2Demo
3 |
4 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wlj32011/dagger2demo/HEAD/Dagger2Demo/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wlj32011/dagger2demo/HEAD/Dagger2Demo/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Dagger2Demo/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | /.idea
10 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wlj32011/dagger2demo/HEAD/Dagger2Demo/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wlj32011/dagger2demo/HEAD/Dagger2Demo/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wlj32011/dagger2demo/HEAD/Dagger2Demo/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/scope/b/BModule.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo.scope.b;
2 |
3 | import dagger.Module;
4 |
5 | /**
6 | * Created by wanglj on 16/6/22.
7 | */
8 | @Module
9 | public class BModule {
10 | }
11 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/scope/c/CModule.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo.scope.c;
2 |
3 | import dagger.Module;
4 |
5 | /**
6 | * Created by wanglj on 16/6/22.
7 | */
8 | @Module
9 | public class CModule {
10 | }
11 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/scope/a/AModule.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo.scope.a;
2 |
3 | import dagger.Module;
4 |
5 | /**
6 | * Created by wanglj on 16/6/22.
7 | */
8 | @Module
9 | public class AModule {
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/scope/a/AComponent.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo.scope.a;
2 |
3 | import dagger.Subcomponent;
4 |
5 | /**
6 | * Created by wanglj on 16/6/22.
7 | */
8 | @Subcomponent(modules = AModule.class)
9 | public interface AComponent {
10 | void inject(AActivity aActivity);
11 | }
12 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/scope/c/CComponent.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo.scope.c;
2 |
3 |
4 | import dagger.Subcomponent;
5 |
6 | /**
7 | * Created by wanglj on 16/6/22.
8 | */
9 | @Subcomponent(modules = CModule.class)
10 | public interface CComponent {
11 | void inject(CActivity cActivity);
12 | }
13 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/scope/b/BComponent.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo.scope.b;
2 |
3 | import com.iiseeuu.dagger2demo.scope.UserScope;
4 |
5 | import dagger.Subcomponent;
6 |
7 | /**
8 | * Created by wanglj on 16/6/22.
9 | */
10 | @Subcomponent(modules = BModule.class)
11 | public interface BComponent {
12 | void inject(BActivity bActivity);
13 | }
14 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/AComponent.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo;
2 |
3 | import dagger.Component;
4 |
5 | /**
6 | * Created by wanglj on 16/6/22.
7 | */
8 | @Component(modules = {AModule.class,GsonModule.class})
9 | public interface AComponent {
10 | void inject(MainActivity mainActivity);
11 | void inject(MainActivity2 mainActivity2);
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/scope/AppComponent.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo.scope;
2 |
3 | import javax.inject.Singleton;
4 |
5 | import dagger.Component;
6 |
7 | /**
8 | * Created by wanglj on 16/6/22.
9 | */
10 | @Singleton
11 | @Component(modules = AppModule.class)
12 | public interface AppComponent {
13 | UserComponent plus(UserModule userModule);
14 | }
15 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/AModule.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo;
2 |
3 | import javax.inject.Singleton;
4 |
5 | import dagger.Module;
6 | import dagger.Provides;
7 |
8 | /**
9 | * Created by wanglj on 16/6/22.
10 | */
11 | @Module
12 | public class AModule {
13 |
14 | @Provides
15 | public A provideA(){
16 | return new A();
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/scope/UserScope.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo.scope;
2 |
3 | import java.lang.annotation.Retention;
4 | import java.lang.annotation.RetentionPolicy;
5 |
6 | import javax.inject.Scope;
7 |
8 | /**
9 | * Created by wanglj on 16/6/22.
10 | */
11 | @Scope
12 | @Retention(RetentionPolicy.RUNTIME)
13 | public @interface UserScope {
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/GsonModule.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo;
2 |
3 | import com.google.gson.Gson;
4 |
5 | import dagger.Module;
6 | import dagger.Provides;
7 |
8 | /**
9 | * Created by wanglj on 16/6/22.
10 | */
11 | @Module
12 | public class GsonModule {
13 |
14 | @Provides
15 | public Gson provideGson(){
16 | return new Gson();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/A.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo;
2 |
3 | import android.util.Log;
4 |
5 | import javax.inject.Inject;
6 |
7 | /**
8 | * Created by wanglj on 16/6/22.
9 | */
10 |
11 | public class A {
12 | public String field;
13 |
14 |
15 | @Inject
16 | public A(){
17 |
18 | }
19 |
20 |
21 | public void doSomething(){
22 | Log.e("A", "do something"+field);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/scope/UserNamed.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo.scope;
2 |
3 | import java.lang.annotation.Retention;
4 | import java.lang.annotation.RetentionPolicy;
5 |
6 | import javax.inject.Named;
7 | import javax.inject.Qualifier;
8 |
9 | /**
10 | * Created by wanglj on 16/6/23.
11 | */
12 | @Qualifier
13 | @Retention(RetentionPolicy.RUNTIME)
14 | public @interface UserNamed {
15 | String value() default "";
16 | }
17 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
13 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/test/java/com/iiseeuu/dagger2demo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/scope/User.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo.scope;
2 |
3 | /**
4 | * Created by wanglj on 16/6/22.
5 | */
6 |
7 | public class User {
8 | private String name;
9 | private String avatar;
10 |
11 | public String getName() {
12 | return name;
13 | }
14 |
15 | public void setName(String name) {
16 | this.name = name;
17 | }
18 |
19 | public String getAvatar() {
20 | return avatar;
21 | }
22 |
23 | public void setAvatar(String avatar) {
24 | this.avatar = avatar;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/scope/AppModule.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo.scope;
2 |
3 | import android.app.Application;
4 |
5 | import javax.inject.Singleton;
6 |
7 | import dagger.Module;
8 | import dagger.Provides;
9 |
10 | /**
11 | * Created by wanglj on 16/6/22.
12 | */
13 | @Module
14 | public class AppModule {
15 | private Application application;
16 |
17 | public AppModule(Application application) {
18 | this.application = application;
19 | }
20 |
21 | @Provides
22 | @Singleton
23 | public Application provideApplication() {
24 | return application;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/res/layout/activity_a.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
17 |
18 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/res/layout/activity_b.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
17 |
18 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/res/layout/activity_c.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
17 |
18 |
--------------------------------------------------------------------------------
/Dagger2Demo/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.2.0-alpha1'
9 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
10 |
11 | // NOTE: Do not place your application dependencies here; they belong
12 | // in the individual module build.gradle files
13 | }
14 | }
15 |
16 | allprojects {
17 | repositories {
18 | jcenter()
19 | }
20 | }
21 |
22 | task clean(type: Delete) {
23 | delete rootProject.buildDir
24 | }
25 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/scope/UserModule.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo.scope;
2 |
3 | import dagger.Module;
4 | import dagger.Provides;
5 |
6 | /**
7 | * Created by wanglj on 16/6/22.
8 | */
9 | @Module
10 | public class UserModule {
11 | private User userA;
12 | private User userB;
13 |
14 | public UserModule(User userA,User userB) {
15 | this.userB = userB;
16 | this.userA = userA;
17 | }
18 |
19 | @UserNamed("a")
20 | @Provides
21 | @UserScope
22 | User provideUserA(){
23 | return userA;
24 | }
25 | @UserNamed("b")
26 | @Provides
27 | @UserScope
28 | User provideUserB(){
29 | return userB;
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/wanglj/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/scope/UserComponent.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo.scope;
2 |
3 | import com.iiseeuu.dagger2demo.scope.a.AComponent;
4 | import com.iiseeuu.dagger2demo.scope.a.AModule;
5 | import com.iiseeuu.dagger2demo.scope.b.BComponent;
6 | import com.iiseeuu.dagger2demo.scope.b.BModule;
7 | import com.iiseeuu.dagger2demo.scope.c.CComponent;
8 | import com.iiseeuu.dagger2demo.scope.c.CModule;
9 |
10 | import dagger.Subcomponent;
11 |
12 | /**
13 | * Created by wanglj on 16/6/22.
14 | */
15 | @UserScope
16 | @Subcomponent(modules = UserModule.class)
17 | public interface UserComponent {
18 | AComponent plus(AModule aModule);
19 | BComponent plus(BModule bModule);
20 | CComponent plus(CModule cModule);
21 | }
22 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/MainActivity2.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.util.Log;
7 |
8 | import javax.inject.Inject;
9 |
10 | /**
11 | * Created by wanglj on 16/6/22.
12 | */
13 |
14 | public class MainActivity2 extends AppCompatActivity {
15 |
16 | @Inject
17 | A a;
18 |
19 | @Override
20 | protected void onCreate(@Nullable Bundle savedInstanceState) {
21 | super.onCreate(savedInstanceState);
22 | setContentView(R.layout.activity_main);
23 | DaggerAComponent.builder().aModule(new AModule()).build().inject(this);
24 | Log.e("mainactivity2","a = "+a);
25 |
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/res/layout/activity_login.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
13 |
18 |
19 |
24 |
25 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/androidTest/java/com/iiseeuu/dagger2demo/ExampleInstrumentationTest.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.filters.MediumTest;
6 | import android.support.test.runner.AndroidJUnit4;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 |
11 |
12 | import static org.junit.Assert.*;
13 |
14 | /**
15 | * Instrumentation test, which will execute on an Android device.
16 | *
17 | * @see Testing documentation
18 | */
19 | @MediumTest
20 | @RunWith(AndroidJUnit4.class)
21 | public class ExampleInstrumentationTest {
22 | @Test
23 | public void useAppContext() throws Exception {
24 | // Context of the app under test.
25 | Context appContext = InstrumentationRegistry.getTargetContext();
26 |
27 | assertEquals("com.iiseeuu.dagger2demo", appContext.getPackageName());
28 | }
29 | }
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/scope/App.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo.scope;
2 |
3 | import android.app.Application;
4 | import android.content.Context;
5 |
6 | /**
7 | * Created by wanglj on 16/6/22.
8 | */
9 |
10 | public class App extends Application{
11 | private AppComponent appComponent;
12 | private UserComponent userComponent;
13 |
14 | public static App get(Context context) {
15 | return (App) context.getApplicationContext();
16 | }
17 |
18 | @Override
19 | public void onCreate() {
20 | super.onCreate();
21 | appComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build();
22 | }
23 |
24 | public UserComponent getUserComponent() {
25 | return userComponent;
26 | }
27 |
28 | public UserComponent createUserComponent(User userA,User userB) {
29 | userComponent = appComponent.plus(new UserModule(userA,userB));
30 | return userComponent;
31 | }
32 |
33 | public void releaseUserComponent() {
34 | userComponent = null;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/scope/c/CActivity.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo.scope.c;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.view.View;
8 |
9 | import com.iiseeuu.dagger2demo.MainActivity;
10 | import com.iiseeuu.dagger2demo.R;
11 | import com.iiseeuu.dagger2demo.scope.App;
12 |
13 | /**
14 | * Created by wanglj on 16/6/22.
15 | */
16 |
17 | public class CActivity extends AppCompatActivity{
18 | @Override
19 | protected void onCreate(@Nullable Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 | setContentView(R.layout.activity_c);
22 |
23 |
24 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
25 | @Override
26 | public void onClick(View v) {
27 | App.get(CActivity.this).releaseUserComponent();
28 | startActivity(new Intent(CActivity.this, MainActivity.class));
29 | finish();
30 | }
31 | });
32 |
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'com.neenbedankt.android-apt'
3 |
4 | android {
5 | compileSdkVersion 23
6 | buildToolsVersion "23.0.3"
7 | defaultConfig {
8 | applicationId "com.iiseeuu.dagger2demo"
9 | minSdkVersion 15
10 | targetSdkVersion 23
11 | versionCode 1
12 | versionName "1.0"
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 | }
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 | lintOptions {
22 | warning 'InvalidPackage'
23 | }
24 | }
25 |
26 | dependencies {
27 | compile fileTree(include: ['*.jar'], dir: 'libs')
28 | compile 'com.android.support:appcompat-v7:23.4.0'
29 | compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha1'
30 | testCompile 'junit:junit:4.12'
31 | androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
32 | androidTestCompile 'com.android.support.test:runner:0.5'
33 | androidTestCompile 'com.android.support:support-annotations:23.4.0'
34 | apt 'com.google.dagger:dagger-compiler:2.2'
35 | provided 'org.glassfish:javax.annotation:10.0-b28'
36 | compile 'com.google.dagger:dagger:2.2'
37 | compile 'com.google.code.gson:gson:2.7'
38 | }
39 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/scope/b/BActivity.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo.scope.b;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.support.annotation.Nullable;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.util.Log;
9 | import android.view.View;
10 | import android.widget.TextView;
11 |
12 | import com.iiseeuu.dagger2demo.R;
13 | import com.iiseeuu.dagger2demo.scope.App;
14 | import com.iiseeuu.dagger2demo.scope.User;
15 | import com.iiseeuu.dagger2demo.scope.UserNamed;
16 | import com.iiseeuu.dagger2demo.scope.c.CActivity;
17 |
18 | import javax.inject.Inject;
19 |
20 | /**
21 | * Created by wanglj on 16/6/22.
22 | */
23 |
24 | public class BActivity extends AppCompatActivity{
25 | @UserNamed("a")
26 | @Inject
27 | User userA;
28 |
29 | @UserNamed("b")
30 | @Inject
31 | User userB;
32 |
33 |
34 | @Override
35 | protected void onCreate(@Nullable Bundle savedInstanceState) {
36 | super.onCreate(savedInstanceState);
37 | setContentView(R.layout.activity_b);
38 | App.get(this).getUserComponent().plus(new BModule()).inject(this);
39 | TextView textView = (TextView) findViewById(R.id.text);
40 | textView.setText("username:"+userA.getName()+"--user:"+userA+""+"username:"+userB.getName()+"--user:"+userB);
41 |
42 |
43 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
44 | @Override
45 | public void onClick(View v) {
46 | startActivity(new Intent(BActivity.this, CActivity.class));
47 | }
48 | });
49 | }
50 |
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/scope/a/AActivity.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo.scope.a;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.support.annotation.Nullable;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.util.Log;
9 | import android.view.View;
10 | import android.widget.TextView;
11 |
12 | import com.iiseeuu.dagger2demo.MainActivity;
13 | import com.iiseeuu.dagger2demo.R;
14 | import com.iiseeuu.dagger2demo.scope.App;
15 | import com.iiseeuu.dagger2demo.scope.User;
16 | import com.iiseeuu.dagger2demo.scope.UserNamed;
17 | import com.iiseeuu.dagger2demo.scope.b.BActivity;
18 |
19 | import javax.inject.Inject;
20 |
21 | /**
22 | * Created by wanglj on 16/6/22.
23 | */
24 |
25 | public class AActivity extends AppCompatActivity{
26 | @UserNamed("a")
27 | @Inject
28 | User userA;
29 |
30 | @UserNamed("b")
31 | @Inject
32 | User userB;
33 |
34 | @Override
35 | protected void onCreate(@Nullable Bundle savedInstanceState) {
36 | super.onCreate(savedInstanceState);
37 | setContentView(R.layout.activity_a);
38 | //注入
39 | App.get(this).getUserComponent().plus(new AModule()).inject(this);
40 | TextView textView = (TextView) findViewById(R.id.text);
41 | textView.setText("username:"+userA.getName()+"--user:"+userA+""+"username:"+userB.getName()+"--user:"+userB);
42 |
43 |
44 |
45 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
46 | @Override
47 | public void onClick(View v) {
48 | startActivity(new Intent(AActivity.this, BActivity.class));
49 | }
50 | });
51 |
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/Dagger2Demo/app/src/main/java/com/iiseeuu/dagger2demo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.iiseeuu.dagger2demo;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.view.View;
7 | import android.widget.EditText;
8 |
9 | import com.iiseeuu.dagger2demo.scope.App;
10 | import com.iiseeuu.dagger2demo.scope.User;
11 | import com.iiseeuu.dagger2demo.scope.a.AActivity;
12 |
13 |
14 |
15 | public class MainActivity extends AppCompatActivity {
16 |
17 | // @Inject
18 | // A a;
19 | //
20 | // @Inject
21 | // Gson gson;
22 |
23 |
24 | @Override
25 | protected void onCreate(Bundle savedInstanceState) {
26 | super.onCreate(savedInstanceState);
27 | setContentView(R.layout.activity_login);
28 |
29 | // DaggerAComponent.builder().aModule(new AModule()).build().inject(this);
30 |
31 | // a.field = "test";
32 | // String aStr = gson.toJson(a);
33 | // Log.e("mainactivity","astr = "+aStr);
34 | // a.doSomething();
35 | // Log.e("mainactivity","a = "+a);
36 | final EditText etUserName = (EditText) findViewById(R.id.username);
37 | final EditText etPassword = (EditText) findViewById(R.id.password);
38 |
39 |
40 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
41 | @Override
42 | public void onClick(View v) {
43 |
44 | User userA = new User();
45 | userA.setName(etUserName.getText().toString()+"AAA");
46 | userA.setAvatar(etPassword.getText().toString());
47 |
48 | User userB = new User();
49 | userB.setName(etUserName.getText().toString()+"BBB");
50 | userB.setAvatar(etPassword.getText().toString());
51 |
52 | App.get(MainActivity.this).createUserComponent(userA,userB);
53 | startActivity(new Intent(MainActivity.this, AActivity.class));
54 | }
55 | });
56 |
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ###前言
2 | 依赖注入概念网络有很多解释,这里就不详细介绍,本文通过一个简单的示例一步步深入了解依赖注入的优势以及为什么使用依赖注入。
3 | ###概念
4 | 依赖注入(Dependency Injection),简称DI,又叫控制反转(Inversion of Control),简称IOC。
5 | 当一个类的实例需要另另一个类的实例进行协助时,在传统的设计中,通常由调用者来创建被调用者的实例,然而依赖注入的方式,创建被调用者不再由调用者创建实例,创建被调用者的实例的工作由IOC容器来完成,然后注入到调用者。因此也被称为依赖注入。
6 | ###作用
7 | 将各层的对象以松耦合的方式组织在一起,解耦,各层对象的调用完全面向接口。当系统重构或者修改的时候,代码的改写量将大大减少。
8 |
9 | ####Android Studio 引入Dagger2
10 | 1.在工程根目录的build.gradle引入apt插件
11 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
12 | 整体文件:
13 | ```gradle
14 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
15 |
16 | buildscript {
17 | repositories {
18 | jcenter()
19 | }
20 | dependencies {
21 | classpath 'com.android.tools.build:gradle:2.2.0-alpha1'
22 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
23 |
24 | // NOTE: Do not place your application dependencies here; they belong
25 | // in the individual module build.gradle files
26 | }
27 | }
28 |
29 | allprojects {
30 | repositories {
31 | jcenter()
32 | }
33 | }
34 |
35 | task clean(type: Delete) {
36 | delete rootProject.buildDir
37 | }
38 | ```
39 |
40 | 2.在app目录下的build.gradle加上一行
41 |
42 | apply plugin: 'com.neenbedankt.android-apt'
43 |
44 | dependencies下面加上:
45 | apt 'com.google.dagger:dagger-compiler:2.2'
46 | provided 'org.glassfish:javax.annotation:10.0-b28'
47 | compile 'com.google.dagger:dagger:2.2'
48 |
49 | 别忘了加入lint warning
50 | lintOptions {
51 | warning 'InvalidPackage'
52 | }
53 |
54 |
55 | 整体文件:
56 | ```gradle
57 | apply plugin: 'com.android.application'
58 | apply plugin: 'com.neenbedankt.android-apt'
59 |
60 | android {
61 | compileSdkVersion 23
62 | buildToolsVersion "23.0.3"
63 | defaultConfig {
64 | applicationId "com.iiseeuu.dagger2demo"
65 | minSdkVersion 15
66 | targetSdkVersion 23
67 | versionCode 1
68 | versionName "1.0"
69 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
70 | }
71 | buildTypes {
72 | release {
73 | minifyEnabled false
74 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
75 | }
76 | }
77 | lintOptions {
78 | warning 'InvalidPackage'
79 | }
80 | }
81 |
82 | dependencies {
83 | compile fileTree(include: ['*.jar'], dir: 'libs')
84 | compile 'com.android.support:appcompat-v7:23.4.0'
85 | compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha1'
86 | testCompile 'junit:junit:4.12'
87 | androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
88 | androidTestCompile 'com.android.support.test:runner:0.5'
89 | androidTestCompile 'com.android.support:support-annotations:23.4.0'
90 | apt 'com.google.dagger:dagger-compiler:2.2'
91 | provided 'org.glassfish:javax.annotation:10.0-b28'
92 | compile 'com.google.dagger:dagger:2.2'
93 | compile 'com.google.code.gson:gson:2.7'
94 | }
95 |
96 | ```
97 |
98 |
99 | ####@Inject介绍
100 | 注解(Annotation)来标注目标类中所依赖的其他类,同样用注解来标注所依赖的其他类的构造函数,那注解的名字就叫Inject
101 |
102 |
103 | **使用dagger2之前**
104 | ```java
105 | public class A {
106 | private String field;
107 |
108 | public A(){
109 |
110 | }
111 |
112 | public void doSomething(){
113 | Log.e("A", "do something");
114 | }
115 | }
116 | //Activity调用
117 | public class MainActivity extends AppCompatActivity {
118 |
119 | A a;
120 |
121 | @Override
122 | protected void onCreate(Bundle savedInstanceState) {
123 | super.onCreate(savedInstanceState);
124 | setContentView(R.layout.activity_main);
125 | a = new A();
126 | a.doSomething();
127 | }
128 | }
129 | ```
130 | **使用dagger2之后**
131 | ```java
132 | public class A {
133 | private String field;
134 |
135 | @Inject
136 | public A(){
137 |
138 | }
139 |
140 | public void doSomething(){
141 | Log.e("A", "do something");
142 | }
143 | }
144 | //Activity调用
145 | public class MainActivity extends AppCompatActivity {
146 |
147 | @Inject
148 | A a;
149 |
150 | @Override
151 | protected void onCreate(Bundle savedInstanceState) {
152 | super.onCreate(savedInstanceState);
153 | setContentView(R.layout.activity_main);
154 |
155 | a.doSomething();
156 | }
157 | }
158 | ```
159 | 这样写直接运行的话 是会报错的,变量a没有被实例化,会报空指针错误。因为他们没有任何关联。这个时候我们需要把MainActivity和A类关联起来,这个时候就需要@Commponent了,下面请看@Commponent:
160 |
161 | ####@Commponent
162 | A类的构造函数与调用类Activity都使用Inject注解,Component一般是个接口,就是将MainActivity与A类桥接起来。
163 | 我们定义一个AComponent接口,并使用@Component注解:
164 | ```java
165 | @Component
166 | public interface AComponent {
167 | A a();
168 | }
169 |
170 | //Activity调用
171 | public class MainActivity extends AppCompatActivity {
172 |
173 | @Inject
174 | A a;
175 |
176 | @Override
177 | protected void onCreate(Bundle savedInstanceState) {
178 | super.onCreate(savedInstanceState);
179 | setContentView(R.layout.activity_main);
180 |
181 | a = DaggerAComponent.builder().build().a();
182 |
183 | a.doSomething();
184 | }
185 | }
186 | ```
187 | AComponent会查找MainActivity中用Inject注解标注的属性,查找到相应的属性后会接着查找该属性对应的用Inject标注的构造函数(这时候就发生联系了),剩下的工作就是初始化该属性的实例并把实例进行赋值。因此我们也可以给Component叫另外一个名字注入器(Injector)
188 |
189 | Component现在是一个注入器,就像注射器一样,Component会把A的实例注入到MainActivity中,来初始化MainActivity类中的依赖。
190 |
191 | a = DaggerAComponent.builder().build().a();这种写法不太友好,一般情况下,我们只需要将MainActivity的实例交给AComponent引用即可。
192 |
193 | ```java
194 | public class MainActivity extends AppCompatActivity {
195 |
196 | @Inject
197 | A a;
198 |
199 | @Override
200 | protected void onCreate(Bundle savedInstanceState) {
201 | super.onCreate(savedInstanceState);
202 | setContentView(R.layout.activity_main);
203 |
204 | DaggerAComponent.builder().build().inject(this);
205 |
206 | a.doSomething();
207 | }
208 | }
209 | ```
210 |
211 |
212 | ####@Module
213 | Module的职责是用来生成实例,可以把他比作一个工厂,专业生成各种类的实例。
214 |
215 | 项目中使用到了第三方的类库,第三方类库又不能修改,所以根本不可能把Inject注解加入这些类中,这时我们的Inject就失效了。
216 |
217 | 比如我项目中依赖GSON解析库。这个时候我需要新建一个类一个提供一个Gson的实例。
218 | ```java
219 | @Module
220 | public class AModule {
221 |
222 | @Provides
223 | public Gson provideGson(){
224 | return new Gson();
225 | }
226 | }
227 | //component类 引用module为AModule.class
228 | @Component(modules = AModule.class)
229 | public interface AComponent {
230 | void inject(MainActivity mainActivity);
231 | }
232 |
233 | //activity调用
234 | public class MainActivity extends AppCompatActivity {
235 |
236 | @Inject
237 | A a;
238 |
239 | @Inject
240 | Gson gson;
241 |
242 | @Override
243 | protected void onCreate(Bundle savedInstanceState) {
244 | super.onCreate(savedInstanceState);
245 | setContentView(R.layout.activity_main);
246 |
247 | DaggerAComponent.builder().build().inject(this);
248 |
249 | a.field = "test";
250 | String aStr = gson.toJson(a);
251 | Log.e("mainactivity","astr = "+aStr);
252 | a.doSomething();
253 | }
254 | }
255 | ```
256 | ####@Provides
257 | module类对外提供实例方法的注解
258 |
259 |
260 | ####注意:
261 | 1. component首先先去提供的module里面寻找提供的实例,没有找到时再去找构造函数@inject注解
262 | 2. 一个component可以依赖多个module,一个component可以依赖另一个component
263 | 例如:AModule 提供A类的实例,GsonModule提供Gson的实例
264 | ```java
265 | //AModule.java
266 | @Module
267 | public class AModule {
268 |
269 | @Provides
270 | public A provideA(){
271 | return new A();
272 | }
273 | }
274 | //GsonModule.java
275 | @Module
276 | public class GsonModule {
277 |
278 | @Provides
279 | public Gson provideGson(){
280 | return new Gson();
281 | }
282 | }
283 | //component
284 | @Component(modules = {AModule.class,GsonModule.class})
285 | public interface AComponent {
286 | void inject(MainActivity mainActivity);
287 | }
288 | //调用activity不变
289 | ...
290 | ```
291 | ####@Scope and @Singleton
292 | 这个注解是用来划分作用域的,标记当前对象使用范围。
293 | 比如限制对象只能在所有Activity中使用,或者只能在Application中使用,或者只能在Fragment中使用
294 | @Singleton 单例模式全局共用一个对象 就是@Scope的一个实现。
295 |
296 | 这个Scope比较难以理解,我们举个例子自定义一个Scope:
297 | 假如有个项目包含用户体系,用户登录成功后,A界面、B界面和C界面要依赖用户来获取一些数据,LoginActivity界面不依赖于用户体系。
298 | 我们想要把User对象实例可以在A、B、C界面共用。
299 |
300 | 那么整体项目的Scope划分结果图为:
301 | 
302 |
303 |
304 | 1.自定义UserScope注解
305 | ```java
306 | @Scope
307 | @Retention(RetentionPolicy.RUNTIME)
308 | public @interface UserScope {
309 |
310 | }
311 | ```
312 | 2.新建一个User实体类
313 | ```java
314 | public class User {
315 | private String name;
316 | private String avatar;
317 |
318 | public String getName() {
319 | return name;
320 | }
321 |
322 | public void setName(String name) {
323 | this.name = name;
324 | }
325 |
326 | public String getAvatar() {
327 | return avatar;
328 | }
329 |
330 | public void setAvatar(String avatar) {
331 | this.avatar = avatar;
332 | }
333 | }
334 | ```
335 | 3.新建一个UserModule来提供User的实例,提供实例方法使用自定义的UserScope注解,表示提供实例仅限于UserScope范围内使用。
336 | ```java
337 | @Module
338 | public class UserModule {
339 | private User user;
340 |
341 | public UserModule(User user) {
342 | this.user = user;
343 | }
344 |
345 | @Provides
346 | @UserScope
347 | User provideUser(){
348 | return user;
349 | }
350 |
351 | }
352 | ```
353 | 4.新建Component桥梁。他是一个子Component,依赖于一个全局的父Component。
354 | Subcomponent注解与Component依赖另一个Component有点像,他们区别在于:
355 | Subcomponent可以获取到父Component的所有可以产生出的对象。
356 | Component依赖则只能获取到被依赖的Component所暴露出来的可以生产的对象
357 |
358 | ```java
359 | @UserScope
360 | @Subcomponent(modules = UserModule.class)
361 | public interface UserComponent {
362 | AComponent plus(AModule aModule);
363 | BComponent plus(BModule bModule);
364 | CComponent plus(CModule cModule);
365 | }
366 | ```
367 | 5.新建一个全局的父Component,引用子Component。
368 | ```java
369 | @Singleton
370 | @Component(modules = AppModule.class)
371 | public interface AppComponent {
372 | UserComponent plus(UserModule userModule);
373 | }
374 | ```
375 | 6.新建一个AppModule,提供一个全局的application实例
376 | ```java
377 | @Module
378 | public class AppModule {
379 | private Application application;
380 |
381 | public AppModule(Application application) {
382 | this.application = application;
383 | }
384 |
385 | @Provides
386 | @Singleton
387 | public Application provideApplication() {
388 | return application;
389 | }
390 | }
391 | ```
392 | 7.创建一个App实例,在程序启动时,调用它。
393 | ```java
394 | public class App extends Application{
395 | //application组件
396 | private AppComponent appComponent;
397 | //用户组件
398 | private UserComponent userComponent;
399 |
400 | //获取当前application的实例
401 | public static App get(Context context) {
402 | return (App) context.getApplicationContext();
403 | }
404 |
405 | @Override
406 | public void onCreate() {
407 | super.onCreate();
408 | //注入全局Application
409 | appComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build();
410 | }
411 |
412 | //对外提供UserComponent
413 | public UserComponent getUserComponent() {
414 | return userComponent;
415 | }
416 |
417 | //注入UserComponent,调用此方法后,UserCope生效
418 | public UserComponent createUserComponent(User user) {
419 | userComponent = appComponent.plus(new UserModule(user));
420 | return userComponent;
421 | }
422 | //释放UserComponent组件
423 | public void releaseUserComponent() {
424 | userComponent = null;
425 | }
426 | }
427 |
428 | ```
429 | 8.LoginActivity 点击登录按钮,创建User实例,并开始UserScope生命周期
430 | ```java
431 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
432 | @Override
433 | public void onClick(View v) {
434 |
435 | User user = new User();
436 | user.setName(etUserName.getText().toString());
437 | user.setAvatar(etPassword.getText().toString());
438 |
439 | App.get(MainActivity.this).createUserComponent(user);
440 | startActivity(new Intent(MainActivity.this, AActivity.class));
441 | }
442 | });
443 | ```
444 | 9.AActivity、BActivity、CActivity 使用User对象为同一个User对象,LoginActivity是没有权限使用User对象的。
445 | 下面为详细代码:
446 | ```java
447 | //AActivity.java
448 | public class AActivity extends AppCompatActivity{
449 | @Inject
450 | User user;
451 |
452 | @Override
453 | protected void onCreate(@Nullable Bundle savedInstanceState) {
454 | super.onCreate(savedInstanceState);
455 | setContentView(R.layout.activity_a);
456 | //注入
457 | App.get(this).getUserComponent().plus(new AModule()).inject(this);
458 | TextView textView = (TextView) findViewById(R.id.text);
459 | textView.setText("username:"+user.getName()+"user:"+user);
460 |
461 |
462 |
463 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
464 | @Override
465 | public void onClick(View v) {
466 | startActivity(new Intent(AActivity.this, BActivity.class));
467 | }
468 | });
469 |
470 | }
471 |
472 | }
473 | //BActivity.java
474 | public class BActivity extends AppCompatActivity{
475 | @Inject
476 | User user;
477 |
478 | @Override
479 | protected void onCreate(@Nullable Bundle savedInstanceState) {
480 | super.onCreate(savedInstanceState);
481 | setContentView(R.layout.activity_b);
482 | App.get(this).getUserComponent().plus(new BModule()).inject(this);
483 | TextView textView = (TextView) findViewById(R.id.text);
484 | textView.setText("username:"+user.getName()+"--user:"+user);
485 |
486 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
487 | @Override
488 | public void onClick(View v) {
489 | startActivity(new Intent(BActivity.this, CActivity.class));
490 | }
491 | });
492 | }
493 |
494 |
495 | }
496 |
497 | //CActivity.java
498 | public class CActivity extends AppCompatActivity{
499 | @Override
500 | protected void onCreate(@Nullable Bundle savedInstanceState) {
501 | super.onCreate(savedInstanceState);
502 | setContentView(R.layout.activity_c);
503 |
504 |
505 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
506 | @Override
507 | public void onClick(View v) {
508 | App.get(CActivity.this).releaseUserComponent();
509 | startActivity(new Intent(CActivity.this, MainActivity.class));
510 | finish();
511 | }
512 | });
513 |
514 | }
515 | }
516 |
517 |
518 | ```
519 | 10.最终效果,打印出User对象地址都是同一个地址:user@1524f0fa
520 | 
521 | 
522 | 
523 |
524 | ####@Qualifier and @Named
525 | @Named 其实是@Qualifier的一种实现,弄明白@Qualifier(限定符)基本上也就明白了@Named
526 | @Qualifier(限定符)主要作用是用来区分不同对象实例。
527 |
528 | 假如上面示例系统支持多用户,在Activity中引用了两个不同的User实例,我们该怎么区分呢?
529 |
530 | 1.首先我们自定义一个@Qualifier 名称叫做UserNamed
531 |
532 | ```java
533 | @Qualifier
534 | @Retention(RetentionPolicy.RUNTIME)
535 | public @interface UserNamed {
536 | String value() default "";
537 | }
538 | ```
539 | 2.修改UserModule对外提供两个User实例userA和UserB,并使用@UserNamed注解标识实例
540 | ```java
541 | @Module
542 | public class UserModule {
543 | private User userA;
544 | private User userB;
545 |
546 | public UserModule(User userA,User userB) {
547 | this.userB = userB;
548 | this.userA = userA;
549 | }
550 |
551 | @UserNamed("a")
552 | @Provides
553 | @UserScope
554 | User provideUserA(){
555 | return userA;
556 | }
557 | @UserNamed("b")
558 | @Provides
559 | @UserScope
560 | User provideUserB(){
561 | return userB;
562 | }
563 |
564 | }
565 |
566 | ```
567 | 3.修改App.java的createUserComponent 传入两个实例
568 | ```java
569 | public UserComponent createUserComponent(User userA,User userB) {
570 | userComponent = appComponent.plus(new UserModule(userA,userB));
571 | return userComponent;
572 | }
573 | ```
574 | 4.登录时创建2个实例
575 | ```java
576 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
577 | @Override
578 | public void onClick(View v) {
579 |
580 | User userA = new User();
581 | userA.setName(etUserName.getText().toString()+"AAA");
582 | userA.setAvatar(etPassword.getText().toString());
583 |
584 | User userB = new User();
585 | userB.setName(etUserName.getText().toString()+"BBB");
586 | userB.setAvatar(etPassword.getText().toString());
587 |
588 | App.get(MainActivity.this).createUserComponent(userA,userB);
589 | startActivity(new Intent(MainActivity.this, AActivity.class));
590 | }
591 | });
592 | ```
593 | 5.Activity中的使用:
594 | ```java
595 | public class AActivity extends AppCompatActivity{
596 | @UserNamed("a")
597 | @Inject
598 | User userA;
599 |
600 | @UserNamed("b")
601 | @Inject
602 | User userB;
603 |
604 | @Override
605 | protected void onCreate(@Nullable Bundle savedInstanceState) {
606 | super.onCreate(savedInstanceState);
607 | setContentView(R.layout.activity_a);
608 | //注入
609 | App.get(this).getUserComponent().plus(new AModule()).inject(this);
610 | TextView textView = (TextView) findViewById(R.id.text);
611 | textView.setText("username:"+userA.getName()+"--user:"+userA+""+"username:"+userB.getName()+"--user:"+userB);
612 |
613 |
614 |
615 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
616 | @Override
617 | public void onClick(View v) {
618 | startActivity(new Intent(AActivity.this, BActivity.class));
619 | }
620 | });
621 |
622 | }
623 |
624 | }
625 |
626 | ```
627 |
628 | 我们在回头看下我们自定义的@UserNamed与系统定义的@Named源码的区别大家应该就能明白了~两个类的实现是一样的代码
629 | ```java
630 | //@UserNamed定义:
631 | @Qualifier
632 | @Retention(RetentionPolicy.RUNTIME)
633 | public @interface UserNamed {
634 | String value() default "";
635 | }
636 |
637 | //@Named的定义:
638 | @Qualifier
639 | @Documented
640 | @Retention(RUNTIME)
641 | public @interface Named {
642 |
643 | /** The name. */
644 | String value() default "";
645 | }
646 |
647 | ```
648 |
649 | dagger2 主要功能 就此介绍完毕,如果觉得不错,就尽快用起来吧~
650 |
651 | ####参考:
652 |
653 |
654 | ####demo地址:
655 |
--------------------------------------------------------------------------------