0

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

4

2 回答 2

2

您可以使用实例工厂方法。请参阅Spring 文档中的相应章节

<bean id="bean2" factory-bean="bean1" factory-method="foo"/>
于 2012-10-03T14:58:15.887 回答
0

如果您使用注释,您可以使用:

@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();
    }

}
于 2012-10-03T15:32:15.433 回答