5

Assume you have such class:

@SomeScope
class ServiceScopeManager {

    @Inject
    Dependency1 dependency1;
    @Inject
    Dependency2 dependency2;

    @Inject
    ServiceScopeManager(){
    }

    @Inject
    void init(){
        //do something really important with dependencies
    }
}
  • This class isn't injected to any other class
  • This class isn't provided to any @Provides method in module

As you can see it's high level class and it, for example, may listens for some events in system and performs releasing of his dependencies.

The problem is that this class won't be ever created, because nothing depends on it.
Can i somehow tell dagger to create dependency always on component creation (for example) not when needed as by default? Or maybe with any other way to achive requirements.

Beloo
  • 9,723
  • 7
  • 40
  • 71
  • `Can i somehow tell dagger to create dependency always on component creation` regardless you need that dependency or no? What's the purpose of that? – azizbekian Apr 26 '17 at 14:40
  • Sounds like this is something your Application may want to take ownership of creating. I'm assuming here you just want a high level system event listener that's always active when your application is on. Or possibly whomever creates your dagger components. – Gabe Sechan Apr 26 '17 at 14:46
  • @GabeSechan not just application, because this dependency could be attached to other scopes. I tried to initialize such class via dagger, because it depends on other dependencies, which are created by dagger – Beloo Apr 26 '17 at 15:45
  • If it could be attached to other scopes, then those scopes should be creating it, wouldn't they? – Gabe Sechan Apr 26 '17 at 15:47
  • @GabeSechan yes, so i need to call initialization manually on component creation, like Jeff Bowman suggested. I had looked for any other solution, but seems that only one exists. – Beloo Apr 26 '17 at 15:53
  • @azizbekian as Gabe Sechan truly mentioned this is high-level manager, which depends on parts of system, but nothing depends on it. And it always exists when component exists. – Beloo Apr 26 '17 at 15:57
  • Possible duplicate of [Dagger 2 - how to create/provide a EagerSingleton](https://stackoverflow.com/questions/31101039/dagger-2-how-to-create-provide-a-eagersingleton) – sylwano May 24 '19 at 10:43

1 Answers1

8

No, Dagger doesn't offer any equivalent of Guice's requestInjection or requestStaticInjection, and if you don't refer to your object, Dagger won't even generate a Factory for it or its dependencies. This is generally a good thing, as it allows you to have a tightly-pruned graph, instead of code-generating Factory implementations for every class on the class path with an @Inject annotation.

You're asking Dagger to do too much here: it's a dependency injection framework and won't manage component life cycle like that. Instead you'll have to perform this initialization in your application logic, maybe by creating a FooComponentInitializer or FooComponentStartup class adjacent to and available through FooComponent. That reduces your code to:

FooComponent fooComponent = DaggerFooComponent.create();
fooComponent.getInitializer().initialize();

...which seems straightforward enough to me.

Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251