What is the correct idiom for using the built-in Guice Injection to annotate static instances of java.util.Logger?

I want to start using Guice 3.x to magically enter my instances java.util.logging.Logger.

Here is the code snippet I'm trying to find:

@Singleton
public class Main
{
    @Inject
    private static final Logger logger;

...
}

This does not work.

I get Exception in thread "main" java.lang.NullPointerExceptionno matter what area I use in the ad.

I added a line super.requestStaticInjection(Main.class);to mine modulein the method configure()and started working, but only if I delete the keyword finaland make it static Logger logger.

I would rather leave Logger final, if at all possible.

What is the correct idiomatic way for Guice to do this?

+3
source share
2

, ,

class Dummy {
   @Inject static Foo;
}

requestStaticInjection(Dummy.class);

configureModule.

+3

Guice ,

. , .

, set().

, , , .

, IllegalAccessException, setAccessible (true), .

JSR 330 .

0

All Articles