By default, spring beans are singletons. I am wondering if there is a way to get multiple instances of same bean for processing.
Here is what I do currently
@Configuration
public class ApplicationMain {
@Value("${service.num: not configured}")
private int num;
//more code
@PostConstruct
public void run(){
for (int i = 0; i < num ; i++) {
MyService ser = new MyService(i);
Future<?> tasks = executor.submit(ser);
}
}
}
Here is the Service class
public class MyService implements Runnable {
private String name;
public Myservice(int i){
name=String.ValueOf(i);
}
}
I have simplified my usecase here.
I want to have MyService as spring bean and get as many as possible based on configuartion (which is num
) in the above for-loop? wondering how that is possible.
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…