Using @Singleton without @Provides in Dagger

Viewed 1749

A small example of a module that injects a class Foo:

@Module(complete = false, injects = { Foo.class })
class MyModule { }

class Foo {
    @Inject
    Foo(Bar bar, Baz baz) { }
}

(assuming that Bar and Baz are provided by a different module)

I now want Foo to be a singleton.

I could add a @Provides method and annotate it with @Singleton ...

@Module(complete = false, injects = { Foo.class })
class MyModule {
    @Provides @Singleton Foo provideFoo(Bar bar, Baz baz) {
        return new Foo(bar, baz);
    }
}

... but having to write that constructor invocation myself kind of defeats the purpose of using an injection framework. Is there a shorter way to accomplish this?

1 Answers
Related