- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我注意到许多开发人员要做的一件事是创建一个从Application继承的类,然后通过依赖项注入创建一个组件,该组件实际上包括组成其应用程序的所有模块。这是在onCreate方法中完成的。我觉得这很奇怪。为什么要将每个模块注入Application类并使其全局可用。毕竟,大多数模块(例如演示者)都绑定到单个活动,并且永远不会用于任何其他活动。那么,为什么不只在活动中创建一个组件,而只包含所需的那些模块,在活动中,这将是一个presenter类。
最佳答案
我不确定是否同意以下前提:大多数应用程序在Application#onCreate中创建一个组件,但是我相信大多数应用程序还具有单独的组件,这些组件包含按活动,按碎片或按服务的绑定,以及这些组件/ modules仅存在,并且仅在您使用有问题的特定活动/片段/服务时才进行类加载。
范围和生命周期
Dagger通过单独的组件管理对象生命周期("scope"),每个组件都可以具有自己的模块集。您用一个或多个作用域注释对组件进行注释,然后用相同作用域(或具有该作用域注释和@Inject
注释的构造函数的任何类)注释的任何绑定将只创建一次并存储在组件中。这与Dagger的默认行为相反,Dagger的默认行为是调用@Provides
方法或为每次调用组件方法或每个@Inject
注释字段创建一个新的对象实例。您可以控制创建组件实例的时间,因此可以控制范围的语义:如果要创建名为@PerActivity
的范围注释,并为Android创建的每个Activity实例创建一个新的组件实例,则您可以确保所有标记为@PerActivity
的绑定都将在该Activity的生存期内返回相同的实例。同样,您可以创建一个@UserScope
,其中每个用户都获得一个单独的组件实例。 JSR-330中唯一的标准化范围是@Singleton
,它应适用于整个应用程序。
但是,如果要混合作用域(如具有@PerActivity StatusBarPresenter)取决于@Singleton LoginService,该怎么办? Dagger要求您将它们保留在两个单独的组件中,以便StatusBarPresenter可以在@PerActivity ActivityComponent
中定义,而LoginService可以在@Singleton ApplicationComponent
中定义。您将需要在该ActivityComponent和ApplicationComponent之间建立关系,可以通过具有依赖项的组件或子组件来实现。
具有依赖关系的组件
具有依赖性的组件将分别生成代码,并在dependencies
批注的@Component
属性中列出其依赖性。届时,您需要在其组件上指定该组件的实例
@Singleton @Component(modules = {FooModule.class, BarModule.class})
interface ApplicationComponent {
Foo foo();
// Bar also exists, but is not listed. Let's say Foo uses it internally.
}
@PerActivity @Component(
modules = {BazModule.class},
dependencies = {ApplicationComponent.class})
interface ActivityComponent {
Baz baz();
}
ActivityComponent activityComponent =
DaggerActivityComponent.builder()
.applicationComponent(yourExistingApplicationComponent)
.build();
@Singleton @Component(modules = {FooModule.class, BarModule.class})
interface ApplicationComponent {
Foo foo();
// This is a subcomponent builder method, which can also return a
// @Subcomponent.Builder. More modern code uses the "subcomponents" attribute
// on the @Module annotation.
ActivityComponent createActivityComponent();
}
@PerActivity @Subcomponent(
modules = {BazModule.class},
dependencies = {ApplicationComponent.class})
interface ActivityComponent {
Baz baz();
}
ActivityComponent activityComponent =
yourExistingApplicationComponent.createActivityComponent();
// or, from somewhere that ApplicationComponent injects:
@Inject Provider<ActivityComponent> activityComponentProvider;
ActivityComponent activityComponent = activityComponentProvider.get();
dagger.android
和
@ContributesAndroidInjector
,则使用的子组件的顶部带有一些语法糖。请注意,
@ContributesAndroidInjector
可以使用范围注释进行注释,并且可以采用
modules
的列表,该列表将传递到它生成的子组件。您对
AndroidInjection.inject(this)
的调用将创建这些子组件实例之一,并根据需要对子组件及其模块进行类加载。
关于android - 在Android的Application类中为DI创建所有模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54106557/
在我的 ZF2 应用程序中,我使用 Zend\Di\Di创建我所有的类实例。使用 Zend\Di\Definition\CompilerDefinition 扫描 DI 定义并使用 APC 缓存。这避
为什么人们使用 Spring DI 而不是 JSR330 DI?我看到许多项目仍在高速推进,而 spring DI 却忽视了 JSR330 规范。许多人甚至不知道它的存在。是不是它的营销力度不够,而
我正在使用 spring 制作一个 Web 应用程序,在 web.xml 中我定义了 context-param 来查找 application-context.xml 文件,该文件扫描除 Contr
给定的是 Intel 8086 处理器的汇编程序,它将数组中的数字相加: .model small .stack 100h .data array dw 1,2,3,1,2 sum
.NET 和 Java 都有大量可用的 DI/IoC 容器,并且每个 有许多我发现在不同方面非常有用的模式 与他们合作。我现在正处于我想做等价的地步 JavaScript 中的东西。由于 JavaSc
我有一个使用 Spring 进行 DI 的 Swing 项目,现在我正在尝试迁移到 Eclipse 4 和 OSGi. 使用 Spring 的配置文件,用户可以注释/取消注释 bean,以添加/删除功
Spring 有两种两种类型的 DI:setter DI 和构造 DI。 基于构造函数的 DI 修复了需要注入(inject)依赖项的顺序。基于 Setter 的 DI 不提供此功能。 基于 Sett
TL;博士 在 JCenter 访问 Kodein 核心包是未经授权的。 详情 我们正在使用 Kodein 进行依赖注入(inject),但是当 Gradle 尝试下载任何 org.kodein.*
我已经使用 NInject 一段时间了,现在我将在 Asp.net Core 中开始一个项目。好像NInject cannot be used with Asp.net Core .所以现在我的问题是
目前缺乏有关 DI 主题的文档 - Dependency Injection 。与现有解决方案(Ninject、Autofac、StructureMap)相比,使用内置 DI 有何优点/缺点?默认依赖
我想做的是将两个 Actor (木乃伊 Actor 和爸爸 Actor )传递给小 Actor 。由于使用 actor 引用而不是 actor 是最佳实践,因此我使用 IActorRef 将木乃伊 a
我是 MQL4 的菜鸟,我正在编写我的第一个 EA。 我的目标是获取 ADX 指标的 +DI 和 -DI 的变量。 我使用了 iADX() 函数,如下所示: double a; int OnInit(
我有一个环境,有 4 个相同的设备,我必须连接到这些设备并通过 TCP 连接请求一些参数(每个设备都有其 IP 地址)。 我为需要一些参数的单个设备实现了一个类(如 IP 地址、端口、轮询间隔等...
我正在尝试将 DI(使用 Autofac)引入现有的 Windows 窗体应用程序。 此应用程序有一个基本的插件架构,其中每个插件都显示自己的表单。在启动时,应用程序会扫描已注册的程序集以查找实现 I
我有一个基于 .NET Core API Gateway 的项目。我想引入依赖注入(inject)(di),因为我需要引入的很多包都是基于这种模式的,所以需要使用 IServiceCollection
我正在尝试使用蛋糕模式进行依赖注入(inject),如下所示: trait FooComponent { val foo: Foo trait Foo; } trait AlsoNeedsFo
我即将创建一个桌面应用程序(使用 .NET windows 窗体) 本质上,我想创建一个 n 层应用程序,但我也想要层之间的松散耦合。但是,我不太确定这是否是 Windows 窗体的好方法 现在我只是
我想在我的一个项目中使用依赖注入(inject) (DI)。我写了一个基本的 DI 库,其工作原理如下 let di = new DI(); di.register('screen', Screen)
在: module.directive 'name', -> (scope, element, attr) -> # Whatever implemenation 链接函数的 scope、
我使用这个库https://github.com/smsapi/smsapi-php-client从站点发送短信。但是当我尝试处理服务中的基类时遇到了问题。所以我的问题是有没有办法用参数调用静态方法?
我是一名优秀的程序员,十分优秀!