Alex Rivera | Logout

spring bean with dynamic constructor value

Asked 2012-01-07T19:54:18.677
18

I need to create an Object which is in-complete without the constructor argument. Something like this

Class A  {
  private final int timeOut
  public A(int timeout)
  {
     this.timeOut = timeout;
   }
 //...
}

I would like this Bean to be spring managed, so that I can use Spring AOP later.

<bean id="myBean" class="A" singleton="false">
</bean>

However my bean needs timeout to be passed as a dynamic value - is there a way to create a spring managed bean with dynamic value being injedcted in the constructor?

Edit
Report

1 Answer

1

Do it explicitly:

interface Bean {
    void setTimeout(int timeout);
}

class BeanImpl implements Bean {
    private int timeout;

    @Override
    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }
    ...
}

<bean id="bean" class="BeanImpl" scope="prototype">
    ...
    <!-- Nothing about timeout here -->
    ...
</bean>

class Client {
    private Bean bean;
    public void setBean(Bean bean) {
        this.bean = bean;
    }
    ...
    public void methodThatUsesBean() {
        int timeout = calculateTimeout();
        bean.setTimeout(timeout);
        ...
    }
}
answered 2012-01-07T21:14:07.027

Your Answer