why*_*yem 2 java dependency-injection dagger-2
我正在尝试从 Spring 迁移一个项目,并按照示例 GitHub 存储库开始使用 Dagger 2。
在 Spring 中,我可以在类级别使用注释来公开类的所有方法,包括void
方法。我想在 Dagger 2 中做到这一点,即,我想“提供”一种void
方法。
在下面的代码示例中,也许我应该将printRandomUUID
方法移到Printer
接口中。但是,我正在做这个小练习,目的是迁移经典的 Spring@Component
或@Service
.
正确的做法是什么?是否可以在组件或模块级别提供 void 方法?
public class Main {
interface Printer {
void printMsg(String msg);
}
static class ConsolePrinter implements Printer {
@Override
public void printMsg(String msg) {
System.out.println(msg);
}
}
@Singleton
@Component(modules = ConsoleModule.class)
interface HelloWorldApp {
Printer getPrinter();
//this doesn't compile -> java.lang.IllegalArgumentException: not a valid component method:
void printRandomUUID();
}
@Module
static class ConsoleModule {
@Provides
Printer providePrinter() {
return new ConsolePrinter();
}
//this doesn't compile -> @Provides methods must return a value (not void)
@Provides
void printRandomUUID() {
System.out.println(UUID.randomUUID().toString());
}
}
public static void main(String[] args) {
HelloWorldApp app = DaggerMain_HelloWorldApp.create();
app.getPrinter().printMsg("Hello");
System.out.println("-");
//app.printRandomUUID();//here i want a void method to be consumed, from the component.
}
}
Run Code Online (Sandbox Code Playgroud)
这是不可能的(还)。与 Spring 不同,Dagger 仅通过检查组件接口和模块来配置。这意味着 Dagger@Component
方法必须与@Component
文档中方法的格式相匹配,并且目前无法提供委托给其他实例的任意代码或方法。
这并不是说组件不能有 void 方法:它们可以,但它们必须是单参数方法,@Inject
在外部创建的实例中注入带注释的方法和字段。这些被称为成员注入方法,而不是void
它们也可以返回它们接受的类型以便于链接。
从不同的角度来看,出于简单性和正确性的原因,我认为将任意业务逻辑与 Dagger 创建的组件相结合是一个坏主意:
new
在业务组件中使用一样不合适。(是否应该通过 DI 图提供每个对象是另一天有争议的话题。)如果我像您一样迁移,我会在 HelloWorldApp 旁边创建一个名为 HelloWorldMethods 的类,并将我将放在 HelloWorldApp 上的所有方法转移到该类上。如果这是 Spring 迁移中的常见模式,您甚至可以为它定义一个本地约定(例如,FooComponent 带有 FooMethods 或 FooUtil)。最后,如果您想隐藏 Dagger 实现细节(如在外部 API 中),您还可以编写自己的类来包装和使用您的组件,将重要方法委托给内部组件并提供您需要的任意实现。
归档时间: |
|
查看次数: |
1060 次 |
最近记录: |