我有一项简单的服务:
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();
}
}
有没有办法让匕首注射"服务"我退回之前和它注入其他物体之前?
答案 0 :(得分:1)
好吧,它比我想象的要容易(实际上在匕首主页上显示为一个例子):
@Module(library = true )
public class ServiceModule {
@Provides @Singleton
IAccountService provideAccountStoreService(AccountService service) {
return service;
}
@Provides @Singleton
IAccountDAO accountDAO provideAccountDAO() {
return new AccountDAO();
}
}