Spring order of inner bean creation

James :

I have beans multiple beans that relies on each others. I am trying to figure out what I am doing is the write way to do it? I need to make sure the creation order will be followed etc. Also, if there is a better or alternative way to do it. Thanks

@Bean
public BeanA getBeanA(){

 return BeanA();
}

@Bean
public BeanB getBeanB(){

 return BeanB(getBeanA());
}

@Bean
public BeanC getBeanC(){

 return BeanC(getBeanA(), getBeanB());
}
Jigar Joshi :

The correct way will be as follows. Let DI take the control of wiring for you.

@Bean
public BeanA getBeanA(){
 return BeanA();
}

@Bean
public BeanB getBeanB(BeanA beanA){
 return BeanB(beanA);
}

@Bean
public BeanC getBeanC(BeanA beanA, BeanB beanb){
 return BeanC(beanA, beanB);
}

The whole point of DI is to provide it the objects and declare the dependencies and let it figure out ordering of bean creation and wiring properly.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=154008&siteId=1