[英]Autowire Bean with constructor values
我有一个尝试@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)
关于如何解决此问题有任何想法吗? 我四处张望,但没有发现任何有效的方法。
谢谢
您是否要覆盖超时值?
嗯,这将无法正常工作,不存在带有您指定的@Qualifier
bean。
你应该试试这个:
@Autowired
ClientWrapper(@Value("#{(5*1000)}")int timeout){ // Compiles just fine.
@Value("config['timeout.value'] ?: '5*10000'")
int timeout;
}
那么您只需使用具有不同值的应用程序配置文件来启动应用程序。
您还可以使用@ConfigurationProperties(prefix="timeout")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.