簡體   English   中英

Spring:地圖無法自動裝配問題

[英]Spring: Map could not Autowire issue

@Component
public class BankServicesImpl implements BankServices {
    @Autowired
    private DataRepoImpl db;
}

----------------------------------------------------------------------------

@Component
public class DataRepoImpl implements DataRepo{
    private Map<Integer, Account> repo = new HashMap<Integer,Account>();
}

----------------------------------------------------------------------------

@Component
public class Account {
    private Integer accountID;
    private int balance;
}

代碼完成了這項工作,創建了repo HashMap 對象 ( {} )。 然而,我試圖讓 Spring 生成 repo Map 對象。 所以我將 DataRepoImpl 更改為:

@Component
public class DataRepoImpl implements DataRepo{
    @Autowired
    private Map<Integer, Account> repo;
}

Error:
Nov 08, 2021 11:37:06 PM org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataRepoImpl': Unsatisfied dependency expressed through field 'repo'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.Map<java.lang.Integer, com.doubleliu.model.Account>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataRepoImpl': Unsatisfied dependency expressed through field 'repo'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.Map<java.lang.Integer, com.doubleliu.model.Account>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.Map<java.lang.Integer, com.doubleliu.model.Account>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

但是,當我將 accountID 和 Map 鍵重構為 String 類型時,我能夠生成包含一個奇怪的“虛擬”變量的 HashMap:

{account=com.doubleliu.model.Account@397fbdb}

回到整數,我無法修復錯誤,然后我嘗試將 Autowired 移動到 DataRepoImpl 類的空構造函數:

    @Autowired
    public DataRepoImpl() {
    }

但是我從repo得到 null 值,因為(我的假設)對象還沒有被創建或分配給repo Map 變量。 同樣,我然后將 Autowired 移動到構造函數上,並將 map 作為參數:

    @Autowired
    public DataRepoImpl(Map<Integer, Account> repo) {
        this.repo = repo;
    }

這次創建了repo對象( {} )而不是 null,我假設該對象是通過參數創建的,這是利用 spring 創建它的正確方法嗎? 我的 IDE 也用Could not autowire. No beans of 'Map<Integer, Account>' type found.的錯誤標記我Could not autowire. No beans of 'Map<Integer, Account>' type found. Could not autowire. No beans of 'Map<Integer, Account>' type found. 但我仍然可以編譯它。 我想知道如何修復它..僅供參考,我正在使用 IntelliJ IDEA

我剛開始學習 spring 一點點,試圖在我進入下一個級別之前完全理解。 所以任何響應、解決方案、評論、對代碼的建議、修復和問題都非常滿意,尤其是對自動裝配的。 謝謝你。

編輯:我正在使用基於 xml 注釋的配置

這里相關

我剛剛發現我需要在xml中定義已定義bean的屬性 每次我定義類似 HashMap 的東西時,將以下內容添加到 xml 中,它使我能夠通過在類中的 Map 變量上定義 @Resource 來注入剛剛定義的 Map bean。

<util:map id="repo" scope="prototype" map-class="java.util.HashMap"
                  key-type="java.lang.String" value-type="com.doubleliu.model.Account"/>

感謝您的幫助和評論,歡迎更多的評論和建議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM