简体   繁体   English

模块中的@Singleton与Dagger 2中的组件中的@Singleton之间的差异

[英]Difference between the @Singleton in module and @Singleton in component in Dagger 2

Im learning Dagger 2, i have noticed that in some examples there is a @Singleton in the module's methods and on other @Singleton on the Component's methods? 林学习匕首2,我已经注意到,在一些例子中有一个@Singleton在模块的方法和其他@Singleton对组件的方法? What is the difference and what exactly a @Singleton annotation on a module's method and on an component's method mean? 有什么区别以及模块方法和组件方法的@Singleton注释究竟是什么意思?

Since you're a beginner, I highly recommend just trying stuff. 由于你是初学者,我强烈建议你尝试一下。 Writing unit tests is easy enough, and it helps to understand and prove theories. 编写单元测试很容易,它有助于理解和证明理论。

If you didn't already, read the User's Guide for some basic knowledge about dagger and scopes. 如果您还没有,请阅读用户指南 ,了解有关匕首和示波器的一些基本知识。

Annotating methods in components (provision methods) doesn't have any effect. 注释组件的方法(计提方法)没有任何影响。 You will have to annotate the class or providing method in a module. 您必须在模块中注释类或提供方法。 I want to quickly show how you can just quickly prove this yourself: 我想快速展示你如何能够自己快速证明这一点:

We have 2 Component, one using a scope @Singleton , the other one none: 我们有2个Component,一个使用范围@Singleton ,另一个使用none:

@Singleton
@Component(modules = SingletonModule.class)
public interface SingletonComponent {

    Object getObject();
}

@Component(modules = NormalModule.class)
public interface NormalComponent {

    @Singleton
    Object getObject();

}

With those components come 2 modules, one providing the singleton scoped object (same as the component) the other one just uses no scope: 这些组件有2个模块,一个提供单例范围对象(与组件相同),另一个只使用范围:

@Module
public class SingletonModule {

    @Provides
    @Singleton
    public Object provideObject() {
        return new Object();
    }
}

@Module
public class NormalModule {

    @Provides
    public Object provideObject() {
        return new Object();
    }
}

And now we just create a small test: 现在我们只是创建一个小测试:

public class ComponentTest {

    @Test
    public void testSingletonComponent() {
        SingletonComponent component = DaggerSingletonComponent.create();

        Assert.assertEquals(component.getObject(), component.getObject());
    }


    @Test
    public void testNormalComponent() {
        NormalComponent component = DaggerNormalComponent.create();

        Assert.assertNotSame(component.getObject(), component.getObject());
    }
}

This test will succeed and prove that annotating methods in components doesn't do anything. 此测试将成功并证明组件中的注释方法不起作用。 Scoping objects in modules, or annotating the class itself when using constructor injection will result in the object being reused within the same scope / the same component . 在使用构造函数注入时,在模块中作用域对象或注释类本身将导致对象在同一作用域/同一组件中重用。

Creating 2 components of the same scope will also lead to duplicate objects, as can be proven like this: 创建相同作用域的2个组件也会导致重复的对象,可以这样证明:

@Test
public void testTwoSingleonComponents() {
    SingletonComponent component1 = DaggerSingletonComponent.create();
    SingletonComponent component2 = DaggerSingletonComponent.create();

    Assert.assertNotSame(component1.getObject(), component2.getObject());
}

Be sure to read some tutorials and be sure to try things out. 一定要阅读一些教程,一定要尝试一下。 The compiler will complain if you do things wrong! 如果你做错了,编译器抱怨! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM