Behzad Pirvali
Behzad Pirvali

Reputation: 784

Spring Instantiation using a none-static factory method

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

Answers (2)

sgroh
sgroh

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

Eugene Kuleshov
Eugene Kuleshov

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

Related Questions