square / dagger

A fast dependency injector for Android and Java.

Home Page:https://square.github.io/dagger/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it OK to create an instance of a dependency already in the module constructor

tercanfurkan opened this issue · comments

Using dagger 1.2.2

There is a case that I need to construct my dependency before it is provided to the classes injecting it. Is it possible to create an instance of this dependency already in the module's constuctor?
This dependency is a scoped dependency, so I am worried if doing the following will lead to a leakage.

HERE IS WHAT I WANT TO DO:

@Module(
        injects = { Controller.class},
        complete = false,
        addsTo = InitModule.class
)
public class SubModule {

    public ScopedDependency dep;

    public SubModule() {
        this.dep = new ScopedDependency();
    }

    @Provides
    @Singleton
    public ScopedDependency provideScopedDependency() {
        return dep;
    }
}

INSTEAD OF THIS:

@Module(
        injects = { Controller.class },
        complete = false,
        addsTo = InitModule.class
)
public class SubModule {

    @Provides
    @Singleton
    public ScopedDependency provideScopedDependency() {
        return new ScopedDependency();
    }
}

When I decide to release the scopedGraph (scopedGraph = null), will I leak the ScopedDependency instance if I use the first approach?

Please use stackoverflow.com for usage questions.