spring bean with dynamic constructor value

前端 未结 3 379
天命终不由人
天命终不由人 2020-12-05 03:26

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 t         


        
3条回答
  •  无人及你
    2020-12-05 03:51

    BeanFactory has a getBean(String name, Object... args) method which, according to the javadoc, allows you to specify constructor arguments which are used to override the bean definition's own arguments. So you could put a default value in the beans file, and then specify the "real" runtime values when required, e.g.

    
        
    
    

    and then:

    getBean("myBean", myTimeoutValue);
    

    I haven't tried this myself, but it should work.

    P.S. scope="prototype" is now preferable to singleton="false", which is deprecated syntax - it's more explicit, but does the same thing.

提交回复
热议问题