Dagger2 - 定义范围的位置

Dagger2 - where define the scopes

我有一个小问题想问 Dagger2。 但首先让我展示示例代码:

@Singleton
@Component(module={ApplicationModule.class})
public Interface ApplicationComponent {

}


@Module
public class ApplicationModule {
    @Provides
    public Context provideContext() {
        return context;   
    }
}

我知道组件中的对象现在是 "Singletons".. 我的问题...这对模块有影响吗?模块也是单例吗?

不,除非您也为 @Provides 带注释的提供程序方法指定范围,否则模块不会是单例的。

@Singleton
@Component(module={ApplicationModule.class})
public Interface ApplicationComponent {
    Context context;
}


@Module
public class ApplicationModule {
    @Provides //unscoped, every injection is new instance
    public Context context() {
        return context;   
    }

    @Provides
    @Singleton //scoped, one instance per component
    public Something something() {
        return new Something();
    }
}