不推荐使用Dagger 2.2组件构建器模块方法 [英] Dagger 2.2 component builder module method deprecated
问题描述
我开始使用dagger 2.2,并且不赞成使用组件"构建器中的模块方法.
I started using dagger 2.2 and the module methods in the Component builder are deprecated.
这是我的应用程序组件:
This is my Application component :
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
void inject(Application application);
}
以及应用程序模块:
@Module
public class ApplicationModule {
Application application;
public ApplicationModule(Application application) {
this.application = application;
}
@Provides
@Singleton
Application providesApplication() {
return application;
}
}
这是生成的类:
@Generated(
value = "dagger.internal.codegen.ComponentProcessor",
comments = "https://google.github.io/dagger"
)
public final class DaggerApplicationComponent implements ApplicationComponent {
private DaggerApplicationComponent(Builder builder) {
assert builder != null;
}
public static Builder builder() {
return new Builder();
}
public static ApplicationComponent create() {
return builder().build();
}
@Override
public void inject(Application application) {
MembersInjectors.<Application>noOp().injectMembers(application);
}
public static final class Builder {
private Builder() {}
public ApplicationComponent build() {
return new DaggerApplicationComponent(this);
}
/**
* @deprecated This module is declared, but an instance is not used in the component. This method is a no-op. For more, see https://google.github.io/dagger/unused-modules.
*/
@Deprecated
public Builder applicationModule(ApplicationModule applicationModule) {
Preconditions.checkNotNull(applicationModule);
return this;
}
}
}
如果不使用ComponentBuilder,如何初始化组件?
How do I initialize the component if not with the ComponentBuilder?
推荐答案
您应该阅读为什么不推荐使用的描述. 如果您使用的是像IntelliJ或Android Studio这样的IDE,您只需选择方法,然后在Windows上按 Control + Q 即可阅读包含弃用声明的Javadoc.
You should read the description of why it is deprecated. If you are using an IDE like IntelliJ or Android Studio you can just select the method and hit Control + Q on Windows to read the Javadoc including the deprecation notice.
Javadoc读取:
The Javadoc reads:
@deprecated声明了此模块,但组件中未使用实例.此方法是禁止操作的.有关更多信息,请参见 https://google.github.io/dagger/unused-modules .
从此链接中您可以看到:
And from this link you can see:
Dagger处理器生成组件时,仅需要提供绑定请求的模块和组件依赖项实例.
When the Dagger processor generates components, it only requires instances of modules and component dependencies that are explicitly needed to supply requests for a binding.
- 如果组件中使用的所有模块方法都是静态的,则Dagger完全不需要该模块的实例. Dagger可以直接调用静态方法而无需模块.
- 如果模块不为Component提供任何绑定,则不需要该模块的实例即可构造图形.
可以肯定地说,您可以忽略弃用.旨在通知您未使用的方法和模块.一旦您实际需要/在子图中的某处使用Application
,就将需要该模块,并且弃用警告将消失.
It is safe to say that you can just ignore the deprecation. It is intended to notify you of unused methods and modules. As soon as you actually require / use Application
somewhere in your subgraph the module is going to be needed, and the deprecation warning will go away.
这篇关于不推荐使用Dagger 2.2组件构建器模块方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!