Built-in bindings

As the name suggests, these are bindings that are automatically covered in the injector. Let the injector make these bindings, as trying to bind them yourself is an error. Loggers is one example of this.

  • Loggersjava.util.logging.Logger has a built-in binding in Guice. The binding naturally sets the logger's title to the title of the class into which the logger is being injected:
@Singleton
public class SMSDatabaseLog implements DatabaseLog {

  private final Logger logger;

  @Inject
  public SMSDatabaseLog(Logger logger) {
    this.logger = logger;
  }

  public void loggerException(UnreachableException e) {
    //Below message will be logged to the SMSDatabaseLog by logger.
    logger.warning("SMS Database connection exception, " + e.getMessage()); ...

Get Java 9 Dependency Injection now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.