google / google/guice

MembersInjectors are only called once per type instead of once per instance

Open
#1,229 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
12.7k
Forks
1.7k
Avg merge
11m
Merged PRs (30d)
2

Description

Using Guice 4.2.2
I have set up a custom injector using the example in the [documentation](https://github.com/google/guice/wiki/CustomInjections), registering a `MembersInjector` into the `TypeEncounter` (exactly like in the documentation).

If I have a type requiring custom injection (we're using `Matchers.any()`, so I mean only types that a field was discovered in with my custom annotation, and a `MembersInjector` was registered for the encounter) that was instantiated multiple times - only the first one has the registered `MembersInjector`s called for.

According to the documentation linked above (emphasis mine):

> MembersInjectors and InjectionListeners can be used to receive a callback after Guice has injected an **instance**. [...] Since they're notified once-per-instance, these should execute as quickly as possible.

If I register an `InjectionListener` then everything works - the listener is called once for each instance. Also, obviously, this is not a problem if the injected type is marked as `@Singleton`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.