Injection-time singleton initialization with custom injections

65 views
Skip to first unread message

Roded Bahat

unread,
Mar 4, 2016, 3:18:27 AM3/4/16
to google-guice
Hi,
I've implemented an SLF4 custom injection using the example provided here: https://github.com/google/guice/wiki/CustomInjections. This works fine.
However, when injecting a singleton, I can't seem to find a way of initializing the singleton instance (at the time of injection) when the custom injection is available.
Previously (before using the SLF4 custom injection) initialization in the constructor sufficed. Now however, initialization in the constructor is impossible (obviously) and is seems like injecting the singleton instance into itself injects an instance without the logger (MembersInjectorImpl handles memberInjectors before userMemberInjectors).
Am I doing anything not allowed? Should I go back to non-injected loggers?
Any comments much appreciated.
Thanks

Tavian Barnes

unread,
Mar 8, 2016, 4:15:58 PM3/8/16
to google-guice
> injecting the singleton instance into itself

Why are you doing this?  The logger will be injected before the singleton is injected into any *other* object.  Just use 'this' instead of self-injecting.

Roded Bahat

unread,
Mar 11, 2016, 2:24:57 PM3/11/16
to google-guice
You're right, the self injection truly makes no sense.
However, I'm left with the implication that the logger cannot be used in constructor code.

Tavian Barnes

unread,
Mar 11, 2016, 8:37:49 PM3/11/16
to google-guice
Indeed, as always the constructor is the first thing to run when constructing an object.

I did write a library that allows for constructor injection of slf4j loggers: https://tavianator.com/announcing-sangria/

Roded Bahat

unread,
Mar 12, 2016, 3:02:55 AM3/12/16
to google-guice
That's very cool. Thanks.
Though, I'm wondering if going to such lengths for the purpose of injectable loggers is worthwhile.
There's the matter of aesthetics, but other than that I'm nearing the conclusion that slf4 static loggers are a better fit considering loggers' special standing.
Reply all
Reply to author
Forward
0 new messages