Auto injecting logger with guice

Posted by koss on Stack Overflow See other posts from Stack Overflow or by koss
Published on 2010-05-28T11:12:49Z Indexed on 2010/05/28 13:32 UTC
Read the original article Hit count: 233

Filed under:
|

With reference to Guice's custom injections article, its TypeListener performs a check for InjectLogger.class annotation - which can be optional. Removing that check will inject to all Logger.class types.

class Log4JTypeListener implements TypeListener {
    public <T> void hear(TypeLiteral<T> typeLiteral, TypeEncounter<T> typeEncounter) {
      for (Field field : typeLiteral.getRawType().getDeclaredFields()) {
        if (field.getType() == Logger.class
            && field.isAnnotationPresent(InjectLogger.class)) {
          typeEncounter.register(new Log4JMembersInjector<T>(field));
        }
      }
    }
  }

I'm tempted to remove "&& field.isAnnotationPresent(InjectLogger.class)" from the listener.

If we're using Guice to inject all instances of our Logger, is there any reason not to do it automatically (without need to annotate)?

© Stack Overflow or respective owner

Related posts about java

Related posts about guice