Skip to Content
Java 9 Dependency Injection
book

Java 9 Dependency Injection

by Nilang Patel, Krunal Patel
April 2018
Intermediate to advanced content levelIntermediate to advanced
246 pages
6h 11m
English
Packt Publishing
Content preview from Java 9 Dependency Injection

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()); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Pro CDI 2 in Java EE 8: An In-Depth Guide to Context and Dependency Injection

Pro CDI 2 in Java EE 8: An In-Depth Guide to Context and Dependency Injection

Jan Beernink, Arjan Tijms
Dependency Injection

Dependency Injection

Dhananjay Prasanna

Publisher Resources

ISBN: 9781788296250Supplemental Content