|
BuiltInBindings
More bindings that you can use
Built-in BindingsAlongside explicit and just-in-time bindings additional bindings are automatically included in the injector. Only the injector can create these bindings and attempting to bind them yourself is an error. LoggersGuice has a built-in binding for java.util.logging.Logger, intended to save some boilerplate. The binding automatically sets the logger's name to the name of the class into which the Logger is being injected.. @Singleton
public class ConsoleTransactionLog implements TransactionLog {
private final Logger logger;
@Inject
public ConsoleTransactionLog(Logger logger) {
this.logger = logger;
}
public void logConnectException(UnreachableException e) {
/* the message is logged to the "ConsoleTransacitonLog" logger */
logger.warning("Connect exception failed, " + e.getMessage());
}The InjectorIn framework code, sometimes you don't know the type you need until runtime. In this rare case you should inject the injector. Code that injects the injector does not self-document its dependencies, so this approach should be done sparingly. TypeLiteralsGuice has complete type information for everything it injects. If you're injecting parameterized types, you can inject a TypeLiteral<T> to reflectively tell you the element type. The StageGuice supports a stage enum to differentiate between development and production runs. |
Sign in to add a comment