Skip to main content

Quick Start

In this tutorial we will create an Injector out of multiple DI modules. Then we will use the Injector to create an instance of some service. We will use annotation-based approach for defining modules as it is more readable.

Helper classes#

For the tutorial we will need 2 helper classes. A DataSource class that represents source of some data and a Service class which uses data retrieved from a data source for some processing.

public static final class DataSource {
private final InetSocketAddress address;
private final String dbName;
@Inject
public DataSource(InetSocketAddress address, String dbName) {
this.address = address;
this.dbName = dbName;
}
public String queryData() {
System.out.printf("Querying %s:%s for data\n", address, dbName);
return "data";
}
}
public static final class Service {
private final DataSource dataSource;
public Service(DataSource dataSource) {
this.dataSource = dataSource;
}
public void process() {
String data = dataSource.queryData();
System.out.printf("Processing data: '%s'\n", data);
}
}
note

Notice the @Inject annotation on the DataSource constructor. It will be explained later.

Modules#

We will define two different modules: one for the configuration and another one for the service itself.

public static class ConfigModule extends AbstractModule {
@Provides
InetSocketAddress inetSocketAddress() {
return new InetSocketAddress(1234);
}
@Provides
String dbName() {
return "exampleDB";
}
}

ConfigModule provides both the address for the DataSource as well as a database name.

public static class ServiceModule extends AbstractModule {
@Provides
Service service(DataSource dataSource) {
return new Service(dataSource);
}
}

ServiceModule only provides the Service itself that requires DataSource to be instantiated.

Injector#

Injector is creted out of two previously defined modules. After that, an instance of Service is required from the injector.

public static void main(String[] args) {
Injector injector = Injector.of(new ConfigModule(), new ServiceModule());
Service service = injector.getInstance(Service.class);
service.process();
}

Service requires DataSource to be instantiated, but we have not provided DataSource explicitly in any of the modules. Thanks to the @Inject annotation on DataSource's constructor, injector injected required dependencies and instantiated the DataSource.

The output of service.process() call is:

Querying 0.0.0.0/0.0.0.0:1234:exampleDB for data
Processing data: 'data'