We touched briefly on singleton and no scope objects in this section, so let's delve a little into what scoping is. WireBox's default behavior is to create a new instance of an object each time you request it via creation or injection (Transient/Prototype objects), this is the NO SCOPE scope.
Scopes allow you to customize the object's life span and duration. The singleton scope allows for the creation of only one instance of an object that will live for the entire life span of the injector. WireBox ships with several different life span scopes but you can also create your own custom scopes (please see the custom scopes section). You can also tell WireBox in what scope to place the instance into by annotations or via the configuration binder. We have an entire section dedicated to discovering all the WireBox annotations, but let's get a sneak peek at them and also how to do it via our mapping DSL.
You can tag a cfcomponent
tag or component declaration with a scope={named scope}
annotation that tells WireBox what scope to use
You can have nothing on the cfcomponent
tag or component declaration which denotes the NO SCOPE
You can tag a cfcomponent
tag or component declaration with a singleton annotation
Here are the internal scopes that ship with WireBox:
This is cool! We can now have full control of how objects are persisted via the WireBox injector, we are not constricted to one type of persistence anymore.
Caution If you use a persistence scope that expires after time like session, request, cachebox, etc, you will experience a side effect called scope widening injection. WireBox offers a solution to this side effect via WireBox Providers, which we will cover in detail.
Scope
Description
NOSCOPE
A prototype object that gets created every time it is requested.
PROTOTYPE
A prototype object that gets created every time it is requested.
SINGLETON
Only one instance of the object exists
SESSION
The object will exist in the session scope
APPLICATION
The object will exist in the application scope
REQUEST
The object will exist in the request scope
SERVER
The object will exist in the server scope
CACHEBOX
A object will be time persisted in any CacheBox cache provider