To represent the main concepts and features of ActiveJ Inject, we've created an example that starts with low-level DI concepts and gradually covers more specific advanced features.
To run the examples, you need to clone ActiveJ from GitHub
And import it as a Maven project. Check out tag v4.3. Before running the examples, build the project. These examples are located at activej -> core-inject -> test and named DiFollowUpTest
In this example we have a kitchen, where you can automatically create tasty cookies with wonderful ActiveJ Inject. Before we get to cooking, note that there are several POJOs with default constructors marked with @Inject annotation: Kitchen, Sugar, Butter, Flour, Pastry and Cookie.
Let's bake a Cookie using ActiveJ Inject in a hardcore way. First of all, we need to provide all the ingredients for the cookie: Sugar, Butter and Flour. Next, there is a recipe for Pastry, which includes ingredients (Sugar, Butter and Flour) we already know how to get. Finally, we can add a recipe of how to bake a Cookie.
It's baking time now! Just create the Injector with all these recipes and ask it for your Cookie instance.
This time we will bake a Cookie with a simple DSL. We will bundle our recipes for Sugar, Butter and Flour in a 'cookbook' module.
Instead of creating bindings explicitly and storing them directly in a map, we just bind the recipes in our module and then give it to the injector.
It's time for real Cookie business. Instead of making bindings explicitly, we will use the declarative DSL.
Like in the previous example, we create a cookbook module, but this time all bindings for the ingredients will be created
automatically from the provider methods. These methods are marked with the
Sometimes it happens that you prepare an injection scheme, but this scheme is not a module. But there is a
method which can help you to make a connection between DI entities and your scheme.
If your class provides a scheme, you can use it easily:
Automatic Bind Using
When we created our POJOs, we've marked their constructors with
If a binding depends on a class that has no known binding, injector will try to automatically generate binding for it.
It will search for
@Inject annotation on its constructors, static factory methods or the class itself (in this case
the default constructor is used) and use them as a factory in generated binding.
Since nothing depends on the Cookie binding, by default no bindings will be generated at all. Here we use a plain bind to tell the injector that we want this binding to be present. Thus the whole tree of bindings it depends on will be generated:
Let's be trendy and bake a sugar-free cookie. In order to do so, along with
@Provides annotation, we will also use @Named annotation and provide two different Sugar, Pastry and Cookie factory functions. This approach allows using different instances of the same class. Now we can tell our injector, which of the cookies we need - a usual one or sugar-free.
You can also use ModuleBuilder
Our cookies turned out to be so tasty, that a lot of people want to try them. However, there is a problem, ActiveJ Inject makes instances singleton by default. Yet, we can't sell the same one cookie to all our customers.
Luckily, there is a solution: we can use a custom @ScopeAnnotation
@OrderScope to create
So our cookbook will look as follows:
In this way, only kitchen will remain singleton:
We received 10 orders from our customers, so now we need 10 instances of cookies:
- First, we inject an instance of Kitchen. Now this instance is stored in the root scope injector.
- Next, we create 10 subinjectors which enter
- Each subinjector creates only one instance of Cookie and refers to the single Kitchen instance of their parent root scope.
You can configure the process of how your injector gets instances and transform this process. For example, you can simply add some logging by overriding configure method:
Now you will receive an output which will represent the time when an instance was created and the instance itself.