Narayan
Narayan

Reputation: 169

instantiate a property with return type form a method

Say I have the following class

public class AbcFactory{

    @Autowired
    private Builder1 builder1;

    @Autowired
      private Builder2 builder2;

    public Builder<Employee > getBuilder(Employee employee) {

        if (employee.isMale(employee)) {
            return builder1;
        } else {
            return builder2;
        }
    }

How to get the returnType from AbcFactory.getBuilder() as a property to a another bean id .

something i tried looks like this

  <property name="builder">
             ?????
            </property>

Upvotes: 1

Views: 34

Answers (2)

kuhajeyan
kuhajeyan

Reputation: 11037

try,

<bean id="emp" class="com.pack.Employee"/>


<bean id="factory" class="com.pack.AbcFactory">

</bean>

<bean id="result" class="com.pack.Builder"
       factory-bean="factory" factory-method="getBuilder">
   <constructor-arg ref="emp"/>
</bean>

Upvotes: 1

Thomas Escolan
Thomas Escolan

Reputation: 1135

Aren't you mixing up static configuration (launchtime) with dynamic behavior (runtime). Spring cannot be setup according to a call that did not happen yet. Or maybe "employee" is a bean itself ? See JavaConfig in that case.

Upvotes: 0

Related Questions