Dependency injection simply means that providing dependencies to spesific instances automatically.
And the dependency means instances that the particular instance needs to do it's job.
The simples usage of this library is:
DefaultClass defaultClass = InstanceBuilder.init(DefaultClass.class).build();
This way, we create a defaultClass
instance with all dependencies.
Note that we create dependencies of this instance from scratch.
If we want to pass spesific argument to instance's constructor method, we may use:
We use withArgs(Object... args)
chain method for injecting arguments with constructor
ExampleClass ec = InstanceBuilder
.init(ExampleClass.class)
.withArgs("ConstructorStringArg")
.build();
There is other ways to instantiate classes. For example, setters. We can simply inject dependencies with setters like this:
Firstly, we need a map to store and match setter and their arguments
Map<String,Object> setterMap;
And then, we need to describe our setter methods and their arguments
setterMap.put("setSecretString","Selam");
Finally, we can use our map in withSetters(Map<String,Object> setters)
chain method to create instance
ExampleClass ec = InstanceBuilder
.init(ExampleClass.class)
.withSetters(setterMap)
.build();
We can similarly inject our dependencies directly with fields.
We just use withFields(Map<String,Object> setters)
chain method.
This is inject the spesific fields that named in map with the corresponding object arguments
For more detailed instructions, you can visit online documentation here