![](/img/trans.png)
[英]Spring configuration in Java - Create and use 2 beans of same class NOT using Autowired
[英]Spring (autowired) component use multiple in the same class
我最近已經開始使用Spring。 而且我沒有找到解決問題的方法。
我有一個組成部分:
@Component
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
@Log4j2
public class CompX { ....
我想在(組件)類中使用兩次:
...
@Autowired
private CompX current;
@Autowired
private CompX old;
...
我怎樣才能做到這一點? 總是有以下異常。
“ org.springframework.beans.factory.NoUniqueBeanDefinitionException:沒有符合條件的bean類型...”
這是因為歧義性和spring混淆了要考慮的那個,所以請通過以下限定告訴spring要使用哪個:
@Autowired
@Qualifier("current")
private CompX current;
@Autowired
@Qualifier("old")
private CompX old;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.