Can someone let me know how we can limit the scope in spring bean to a particular number let's say 2 bean per spring IOC container?
If it is possible then using which scope we can achieve this?
Can someone let me know how we can limit the scope in spring bean to a particular number let's say 2 bean per spring IOC container?
If it is possible then using which scope we can achieve this?
There is no such thing provided by Spring out of the box.
Also, the intention of the scope does not mean to restrict the number of a particular type of a bean to a fixed number. The total number of a bean in a certain scope should be dynamic which depends on the number of the 'process' that the scope is related to. The scope intend to provide an isolated environment for certain 'process' such that each 'process' can have their own instance of a bean type.
If you really want to have 2 beans of a certain type exist in your application , just define two bean of them separately and each of them configure as singleton scope (i.e default scope). And use @Qualifier
(see this) to explicitly configure which one to inject into other beans.
@Configuration
public class AppConfig {
@Bean
public FooBean fooBean1(){
return new FooBean();
}
@Bean
public FooBean fooBean2(){
return new FooBean();
}
}