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.
Comments
Post a Comment