Service scopes
Last updated
Last updated
DiVine uses a hierarchy of containers, called the container tree
, that includes the root container
and sub-containers
. You may use sub-containers, when separate parts of your code needs separate dependencies.
By default, service scopes are CONTAINER
, which means that a unique instance is associated for each container, that requests the dependency. If you want a service to be unique in the whole container tree, consider using @Service(scope=SINGLETON)
to indicate, that no matter what container is the code requesting the dependency from, the same instance should be retrieved.
You can use the following methods to request various containers depending on the call context.
If you want to learn more about container contexts, check out the Container contexts
part in Advanced usage
.
The following code is an example, in which container-scoped services come in handy.