Quickstart
Structure
Within DiVine, there are three key concepts: services, containers and dependencies.
Services are class definitions, that share a common logic. Most common services are either singleton (share one instance throughout your program) or transient (have unique instances).
Containers are collections of dependencies. A container is responsible for instantiating a service, managing its lifecycle and handling shared values.
Dependencies are instances of services or given values, that are stored in a container.
Usage
Service definitions
The following code shows an example of a simple service, that is field-injected in another service.
Note that DiVine requires services to have a @Service
annotation, called the service descriptor. This tells the dependency injector, how the annotated service behaves.
The @Inject
annotation tells DiVine, to inject the dependency into the target field.
Global values
You may also store shared values in the container. You can get rid of your magic values in your code. If you want to access a constant, just request it from the container.
Note that these values are shared within the container. If you want parts of your code to share different values for a key, you may use different containers. If you want to share a value accross all of your containers, you may use the global container,.
Improved testing
Using DiVine can improve the way that you can test your applications. By using interfaces as services, you can easily use a mock implementation.
You can register mock implementations in your test files, therefore you do not need to modify anything in your application code for testing.
Inside your application, you can request the UserController
dependency as such:
Inside your test files you could do the following code:
Note that @Service(implementation = MyImpl.class)
tells DiVine, to implement the interface with MyImpl
by default, unless specified otherwise.
Advanced usage
DiVine features a vast range of features that ensure that you have the best experience while using the dependency injector. You may navigate through the following pages that showcase some of the most important utilities, that you can use to improve your code.
Last updated