Spring Framework supports more than one scope for its bean:
- Singleton: Scopes a single bean definition to a single object instance per Spring IoC container.
- Prototype: Scopes a single bean definition to any number of object instances.
And other three scopes if you work with Spring Framework version 2.5.
You must take care while dealing with some of these scopes, specially the prototype/request ones that creates new bean instance for every request. That can harm your web application server as your requests increase and consumes your server memory and resources.
Resource pooling is the best handling for this situation. It is general concept of having a pool or resources with fixed size (can be configurable) and have your resource handler pool out one resource from this pool for every resource request and returns it again in the pool after the request finished. If your pool is empty, then the requested entity will queue until one resource is available for its request in the pool.
By that way you can tune your resources usage based on their types. Some of resource types have more requests to them; you can increase their pool size. Others have less usage; you can decrease their pool size to the proper value.
Spring Framework gives you this ability of resource pooling while creating your POJOs beans. Spring pooling can be applied to any POJO. Spring provides out-of-the-box support for Jakarta Commons Pool, which provides a fairly efficient pooling implementation. You'll need the commons-pool Jar on your application's classpath to use this feature.
Sample configuration is shown below:
... properties omitted
</bean>
<bean id="poolTargetSource"
class="org.springframework.aop.target.CommonsPoolTargetSource">
<property name="targetBeanName" value="businessObjectTarget"/>
<property name="maxSize" value="25"/>
</bean>
<bean id="businessObject"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetSource" ref="poolTargetSource"/>
</bean>
Note that the target object--"businessObjectTarget" in the example--must be a prototype. This allows the PoolingTargetSource implementation to create new instances of the target to grow the pool as necessary.
Reference:
Spring Framework Documentation, Chapter 7, section 7.10.2
No comments:
Post a Comment