java - How to create a spring bean with specific number of maximum instances -


i need create spring prototype bean in server limited ram. 1 option use spring bean mix of singleton , prototype scopes, can specify maximum number of instances , threads.

is there way in spring create multi instance beans? if not how avoid out of memory errors when using spring prototype beans.

if want use spring purposes suggest using factory bean.

your context:

<beans ...>     <bean id="tool" class="com.example.toolfactory"/> </beans> 

an example of factory bean:

public class toolfactory implements factorybean<tool> {     private atomicinteger currentid = new atomicinteger();      @override     public tool getobject() throws exception {         return new tool(currentid.incrementandget());     }      @override     public class<?> getobjecttype() { return tool.class; }      @override     public boolean issingleton() { return false; } }  public class tool {     private final int id;     public tool(int id) { this.id = id; }     public int getid() { return id; } } 

in toolfactory.getobject() can implement logic wish.
can create pool of beans inside factory.
or throw exception when bean count limit reached.

how use spring factorybean?
what's factorybean?


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -