I have web application with several configuration classes.
@Configuration
public class ConfigA {
@Bean(name = "bean_1")
public MyBean getBean1() { /* Some code is here */ }
@Bean(name = "bean_2")
public MyBean getBean2() { /* Some code is here */ }
/* ... */
@Bean(name = "bean_99")
public MyBean getBean99() { /* Some code is here */ }
}
@Configuration
public class ConfigB {
public OtherBean getOtherBean() { /* Some code is here */ }
}
Еach bean of class MyBean is registered in some global context when constructor is called. And OtherBean bean consumes this global context to get all instances of MyBean class. But spring creates OtherBean earlier then several of MyBean beans and I do not know how I can change this behavior.
PS:
- I do not want to inject all MyBean beans directly (explictly) in OtherBean, there are many MyBean beans.
- Now I have a solution: declare annotation @DependsOn({ "bean_1", "bean_2" .... "bean_99" }) on class ConfigB, but I think it is inconvenient (it is almost the same point 1).
- I think a solution could be force spring to create all declared beans (bean_1, ...) as soon as this configuration class (ConfigA) was discovered.