最简单的Dagger2入门教程


Dagger简单入门,具体参考:http://blog.csdn.net/lisdye2/article/details/51942511


例子代码图如下:




标记注解层,app全局层提供依赖注解,activity层业务视图展现

详细使用请参考以上博文说明,人家写得很用心:

下边贴代码:

标记层:

/**
 * Created by Administrator on 2017/3/22.
 */
@Scope
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface PerActivity {
}



/**
 * Created by Administrator on 2017/3/22.
 */
@Scope
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface PerApp {
}


@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface PersonForContext {
}



@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface PersonForName {
}




app层:

public class App {
    private String name;
}


@PerApp
@Component(modules = AppModule.class)
public interface AppConponent {
    //给下一层依赖层提供
    Context getContext();
}



@Module
public class AppModule {
    private Context context;

    public AppModule(Context context) {
        this.context = context;
    }

    @PerApp
    @Provides
    Context providerContext() {
        return context;
    }
}



public class Context {
    private String contextName = "contextName";

    public Context(String context) {
        this.contextName = context;
    }

    public String getContext() {
        return contextName;
    }

    @Override
    public String toString() {
        return contextName;
    }
}




Activity层:

/**
 * Created by Administrator on 2017/3/22.
 */
@PerActivity
//@Singleton
@Component(dependencies = AppConponent.class,modules = MainModule.class)  // 作为桥梁,沟通调用者和依赖对象库
public interface MainComponent {

    //定义注入的方法
    void inject(Main main);

}




@Module   //提供依赖对象的实例
public class MainModule {

//    private String name;
//
//    @Provides
//    String providesName(){
//        return name;
//    }

    String name;

    public MainModule(String name) {
        this.name = name;
    }

    //关键字,标明该方法提供依赖对象
    //@Named("context")
    //@Singleton
    @PersonForContext
    @Provides
    @PerActivity
    Person providerPerson1(Context context){
        //提供Person对象
        return new Person(context);
    }

    @Provides
    String providesName() {
        return name;
    }

    //关键字,标明该方法提供依赖对象
    //@Named("name")
    @PersonForName
    @Provides
    Person providerPerson2(String name){
        //提供Person对象
        return new Person(name);
    }
}





/**
 * Created by Administrator on 2017/3/22.
 */

public class Person {
    private Context context;
    private String name;

    public Person(Context context){
        this.context = context;
        System.out.println("person create!!!");
        System.out.println("context:" + context.getContext());
    }

    public Person(String name) {
        this.name = name;
    }

    public Context getContext() {
        return context;
    }

    public String getName() {
        return name;
    }
}





调用测试层:


public class Main {

    //@Named("context")
    @PersonForContext
    @Inject
    Person person;

    //@Named("name")
    @PersonForName
    @Inject
    Person person2;

    @PersonForContext
    @Inject
    Lazy<Person> lazyPerson;//注入lazy对象

    @PersonForName
    @Inject
    Provider<Person> providerPerson;

    private static AppConponent appConponent;

    void test() {
        appConponent = DaggerAppConponent.builder().appModule(new AppModule(new Context("myContext"))).build();
        MainComponent component = DaggerMainComponent.builder().appConponent(appConponent).mainModule(new MainModule("William")).build();
        //注入
        component.inject(this);
        System.out.println("person.getContext:" + person.getContext());
        System.out.println("person.getName:" + person2.getName());

        // 调用该方法时才会去创建Person,以后每次调用获取的是同一个对象
        System.out.println("lazyPerson.get():" + lazyPerson.get().getContext());
        // 调用该方法时才回去创建Person1,以后每次调用都会重新加载Module中的具体方法,根据Module中的实现,可能相同,可能不相同。
        System.out.println("providerPerson.get():" + providerPerson.get().getName());

    }

    public static void main(String args[]) {
        new Main().test();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值