Java Reference
In-Depth Information
THE PROTOTYPE SCOPE
The prototype scope is the exact opposite of the singleton scope. Every time the bean
is wired to a consuming bean, a new instance is created. This means beans can safely
hold internal state, although they still need to be careful about thread safety.
Although holding state can be a convenient programming model, its scalability is
more limited. Figure 6.3 shows how the number of bean instances can rapidly multi-
ply with the prototype scope.
The singleton scope is the default Blueprint scope for top-level beans (beans that
are immediate children of the <blueprint> element). Inlined beans (bean declara-
tions that are nested within other elements), on the other hand, have a default scope
of prototype. Not only is the scope prototype by default, the scope is always prototype
for beans declared inline. Because Blueprint is designed to be extensible, the schema
does allow you to specify a scope for inlined beans; however, unless you're using a cus-
tom namespace for your particular Blueprint implementation, any value other than
prototype will be ignored, or cause an error creating the Blueprint container.
For example, the following Blueprint snippet declares a top-level bean with a pro-
totype scope:
<bean
class="fancyfoods.department.cheese.offers.DesperateCheeseOffer"
scope="prototype"
id="cheeseOffer" />
<service
interface="fancyfoods.offers.SpecialOffer"
ref="cheeseOffer">
</service>
prototype
A
B
prototype
C
prototype
D
prototype
F
prototype
E
prototype
Figure 6.3 When the prototype scope is used for a bean, every user of the bean is wired to a new instance.
This allows beans to maintain internal state, but it's less scalable.
Search WWH ::




Custom Search