在Dagger中的@Provides方法中使用注入

时间:2014-06-14 14:50:20

标签: java dependency-injection dagger

我有一项简单的服务:

public class AccountService implements IAccountService {

    @Inject
    IAccountDAO accountDAO;

}

本课程由我的模块提供:

@Module(library = true )
public class ServiceModule {
    @Provides @Singleton
    IAccountService provideAccountStoreService(IAccountDAO accountDAO) {
        IAccountService service= new AccountService();
        //Somehow set the accountDAO inside service
        return service;
    }

    @Provides @Singleton
    IAccountDAO accountDAO provideAccountDAO() {
        return new AccountDAO();
    }

}

有没有办法让匕首注射"服务"我退回之前和它注入其他物体之前?

1 个答案:

答案 0 :(得分:1)

好吧,它比我想象的要容易(实际上在匕首主页上显示为一个例子):

@Module(library = true )
public class ServiceModule {
    @Provides @Singleton
    IAccountService provideAccountStoreService(AccountService service) {
        return service;
    }

    @Provides @Singleton
    IAccountDAO accountDAO provideAccountDAO() {
        return new AccountDAO();
    }

}