Skip to content
Ruedi Steinmann edited this page Apr 25, 2015 · 6 revisions

How to start doing dependency injection with Salta.

Getting Started

Salta comes with two APIs:

  • JSR330 API: Salta's own API is based on the standard javax.inject annotations, complemented with a Guice-like configuration API. Since this API minimizes the number of classes added to your classpath, it is recommended for new projects. This Wiki assumes you are using this API. Maven Dependency:

    <dependency>
        <groupId>com.github.ruediste.salta</groupId>
        <artifactId>salta-jsr330</artifactId>
        <version>0.1</version>
    </dependency>
  • Guice API: Salta's implementation of the Guice API. We tried hard to make it a drop-in replacement for Guice. However, Salta is very different from Guice internally, thus the SPI is not provided. Use this to migrate existing projects. Mave Dependency:

    <dependency>
        <groupId>com.github.ruediste.salta</groupId>
        <artifactId>salta-guice</artifactId>
        <version>0.1</version>
    </dependency>

You can download the artifacts without using maven from The Central Repository

To illustrate, we'll start the BillingService class that accepts its dependent interfaces CreditCardProcessor and TransactionLog in its constructor. To make it explicit that the BillingService constructor is invoked by Salta, we add the @Inject annotation:

class BillingService {
  private final CreditCardProcessor processor;
  private final TransactionLog transactionLog;

  @Inject
  BillingService(CreditCardProcessor processor, 
      TransactionLog transactionLog) {
    this.processor = processor;
    this.transactionLog = transactionLog;
  }

  public Receipt chargeOrder(PizzaOrder order, CreditCard creditCard) {
    ...
  }
}

We want to build a BillingService using PaypalCreditCardProcessor and DatabaseTransactionLog. Salta uses bindings to map types to their implementations. A module is a collection of bindings specified using fluent, English-like method calls:

public class BillingModule extends AbstractModule {
  @Override 
  protected void configure() {

     /*
      * This tells Salta that whenever it sees a dependency on a TransactionLog,
      * it should satisfy the dependency using a DatabaseTransactionLog.
      */
    bind(TransactionLog.class).to(DatabaseTransactionLog.class);

     /*
      * Similarly, this binding tells Salta that when CreditCardProcessor is used in
      * a dependency, that should be satisfied with a PaypalCreditCardProcessor.
      */
    bind(CreditCardProcessor.class).to(PaypalCreditCardProcessor.class);
  }
}

The modules are the building blocks of an injector, which is Salta's object-graph builder. First we create the injector, and then we can use that to build the BillingService:

 public static void main(String[] args) {
    /*
     * Salta.createInjector() takes your Modules, and returns a new Injector
     * instance. Most applications will call this method exactly once, in their
     * main() method.
     */
    Injector injector = Salta.createInjector(new BillingModule());

    /*
     * Now that we've got the injector, we can build objects.
     */
    BillingService billingService = injector.getInstance(BillingService.class);
    ...
  }

By building the billingService, we've constructed a small object graph using Salta. The graph contains the billing service and its dependent credit card processor and transaction log.

Clone this wiki locally