-
Notifications
You must be signed in to change notification settings - Fork 37
Shared
Mauro Gadaleta edited this page Jun 19, 2026
·
2 revisions
Choose between shared singleton services and new instances on each retrieval.
In the service container, all services are shared by default. This means that each time you retrieve the service, you'll get the same instance. This is often the behaviour you want, but in some cases, you might want to always get a new instance.
In order to always get a new instance, set the shared setting to false in your service definition:
services:
app.some_not_shared_service:
class: ...
shared: false
# ...import {Definition} from 'node-dependency-injection'
let definition = new Definition(...)
definition.shared = false
container.setDefinition('app.some_not_shared_service', definition)Now, whenever you call container.get('app.some_not_shared_service') or inject this service, you'll receive a new instance.
Copyright © 2023-2024 Mauro Gadaleta