Skip to content

InjectionPointSpecificInjections

Ruedi Steinmann edited this page Mar 27, 2015 · 1 revision

creating instances that depend where they are injected into

Injection Point Specific Injections

Salta allows the creation of instances which depend on where they are injected. Remember that bindings cannot be injection point specific, since they might be reused for different injection points due to scoping.

Thus the instance is created by a CreationRule, which receives a CoreDependencyKey and can return a function creating a Supplier. Register the rule using bindCreationRule(...).

The canonical example is the injection of a logger, initialized with the name of the class it was injected into:

@Override
protected void configure() throws Exception {
  bindCreationRule(new CreationRuleImpl(
    CoreDependencyKey.rawTypeMatcher(Logger.class), 
    key -> () -> Logger.getLogger(key.getRawType().getName())
  ));
}

Clone this wiki locally