-
Notifications
You must be signed in to change notification settings - Fork 5
Quickstart
Robert edited this page Feb 15, 2016
·
5 revisions
Add this dependency to your Maven build:
<dependency>
<groupId>com.vanillasource.jaywire</groupId>
<artifactId>jaywire</artifactId>
<version>1.1.0</version>
</dependency>This will include the basic interfaces and scopes for standalone usage. For integration with Web-frameworks (for Session and Request scopes), go to Integration.
For a simple application a single "Module" object wireing everything together is written this way:
import com.vanillasource.jaywire.standalone.StandaloneModule;
public class MyAppModule extends StandaloneModule {
public Database getDatabase() {
return singleton(() -> new MyAppDatabase(...));
}
public ServiceA getServiceA() {
return singleton(() -> new ServiceA(getDatabase()));
}
public ServiceB getServiceB() {
return singleton(() -> new ServiceB(getDatabase(), getServiceA());
}
}In this example there is a Database, ServiceA and ServiceB, all of them singletons. Singleton here means, that they will only be instantiated once for an MyAppModule instance. To find out more about scopes go to Scopes. Dependencies are "injected" by simply calling the appropriate method and supplying them as parameters to objects.
You can use the MyAppModule then at the "top" of your application to start processing:
public class MyApp {
public static final void main(String[] argv) {
MyAppModule module = new MyAppModule();
module.getServiceB().run();
}
}