How to use Pooled Spring beans instead of Singleton ones?

前端 未结 5 1270
后悔当初
后悔当初 2021-01-14 16:45

For efficiency reasons, I am interested in limiting the number of threads that simultaneously uses the beans of the Spring application context (I don\'t want an unli

5条回答
  •  鱼传尺愫
    2021-01-14 17:25

    Please note the name of the third bean in the spring example:-"businessObject"

    It means this the bean from where you are supposed to access the common pool.

    For your case if you need your own client bean you may have it as follows. But in such a case businessObject is not required.:-

    
    
      ... properties omitted
    
    
    
      
      
    
    
    
     
    
    
    
    Java classes:-
    public class ClientOfTheBusinessObject{
         CommonsPoolTargetSource poolTargetSource;
    //
      public void methodToAccessCommonPool(){
         //The following line gets the object from the pool.If there is nothing left in the pool then the thread will be blocked.(The blocking can be replaced with an exception by changing the properties of the CommonsPoolTargetSource bean)
         MyBusinessObject mbo = (MyBusinessObject)poolTargetSource.getTarget();
         //Do whatever you want to do with mbo
         //the following line puts the object back to the pool
         poolTargetSource.releaseTarget(mbo);
      }
    }
    

提交回复
热议问题