使用构造函数值自动装配Bean

时间:2016-10-26 19:44:29

标签: java spring-boot inversion-of-control autowired

我有一个我正在尝试@Autowire的连接类,它需要一个超时参数

    @Component
    ClientWrapper {
        ...
        ...
        @Autowired
        ClientWrapper(@Value("#{(5*1000)}")int timeout){  // Compiles just fine.
             this.timeout = timeout;
        }
        ...
        ...  // set connection up 
    }

我有第二堂课试图使用ClientWrapper拨打电话。到目前为止,我一直无法弄清楚如何传递timeout参数。

public class testExternalCall {
   @Autowired
   @Qualified("#{new Integer(10000)}")// This is where I need the guidance.  Not sure how to pass 10000 in as a parameter to the constructor
   ClientWrapper client;

   List<Car> cars = client.getAutos();
}

每当我启动应用程序时Spring都告诉我它无法找到依赖项(没有类型为ClientWrapper的限定Bean)

有关如何解决此问题的任何想法?我看了一遍,但没有找到任何有用的东西。

由于

1 个答案:

答案 0 :(得分:0)

您是否试图覆盖超时值?

这确实无法正常工作,没有指出@Qualifier的bean存在。

你应该试试这个:

@Autowired
ClientWrapper(@Value("#{(5*1000)}")int timeout){  // Compiles just fine.
    @Value("config['timeout.value'] ?: '5*10000'")
    int timeout;
}

然后您只需使用具有不同值的应用程序配置文件启动应用程序。

您也可以使用@ConfigurationProperties(prefix="timeout")

相关问题