Reputation: 784
I have got a situation where I would like to create bean2 in Spring config:
beans.xml:
<bean id="bean1" class="...">
<property name="..." ref="..." />
</bean>
bean2 = bean1.foo()
Would appreciate any help,
Thanks, Behzad
Upvotes: 0
Views: 191
Reputation: 354
If you are using annotations you can use:
@Configuration
public class AppConfig {
@Bean
@Lazy
public Bean1 getBean1(){
return Bean1.getInstance();
}
@Bean
public Bean2 getBean2() {
return this.getBean1().newBean2(); //in your example is this.getBean1().foo();
}
}
Upvotes: 0
Reputation: 31795
You can use instance factory method. See corresponding chapter in Spring documentation.
<bean id="bean2" factory-bean="bean1" factory-method="foo"/>
Upvotes: 2