imgn inject API GitHub

@Provides Methods

The problem

Constructor injection requires annotating the class itself. That is impossible for types you do not own (String, Clock, anything from a library) and awkward for values that need assembly logic — reading configuration, choosing a variant, composing several inputs into one object.

How it solves it

A method annotated @Provides becomes a binding: its return type (plus qualifier, if any) is the key, its body is the recipe, and its parameters are injected like constructor parameters.

public final class Config {
    private Config() {}

    @Provides
    @Named("greeting")
    static String greeting() {
        return "Hello";
    }

    @Provides
    static Clock clock() {
        return Clock.systemUTC();
    }
}

Any class can host @Provides methods:

@Provides methods compose with the rest of the framework: they accept qualified parameters, can be scoped (@Singleton on the method), can contribute to collections (@IntoSet / @IntoMap), can be conditional, and can serve as assisted-injection recipes.

public final class Wiring {
    @Provides
    @Singleton
    static DataSource dataSource(@Named("jdbc.url") String url) {
        return DataSource.open(url);
    }
}

Limitations