【问题标题】:Spring: Map could not Autowire issueSpring:地图无法自动装配问题
【发布时间】:2021-11-08 17:23:59
【问题描述】:
@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 得到空值,因为(我的假设)对象尚未创建或分配给 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&lt;Integer, Account&gt;' type found. 但我仍然可以编译它。我想知道如何解决它.. 仅供参考,我正在使用 IntelliJ IDEA

我刚刚开始学习spring,在进入下一个级别之前尝试完全理解。因此,任何响应、解决方案、cmets、对代码的建议、修复和问题都非常满意,尤其是自动接线。 谢谢。

编辑:我正在使用基于 xml 注释的配置

【问题讨论】:

  • XML 和 Java 的东西是等价的。对于这个 Java 配置,您可以创建一个等效的 XML 配置
  • 无关,但需要考虑的重要一点:从您的示例中,我认为 Account 本身不应该是 Spring bean,而应该是一个实体(您通常会将其保存在数据库中)。
  • @dunni 嘿,我不太确定,但 Account 实际上是用于 oop 的 java pojo 类。我也将它参考 youtube 中的教程.. 我错过了什么吗?
  • 程序到接口而不是类而不是 DataRepoImpl 你应该注入 DataRepo。关于您的Map,您需要定义该类型的bean,spring 不会自动为@Autowired 字段创建bean,它们需要出现在应用程序上下文中。但是在这里注入地图并没有增加太多。

标签: java spring spring-boot


【解决方案1】:

here相关

我刚刚发现我需要在xml中定义已定义bean的properties。每次我定义类似 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"/>

感谢cmets的帮助,欢迎大家多多指教。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 2017-12-18
    • 2016-09-27
    • 2013-01-29
    • 1970-01-01
    • 2011-12-29
    • 2023-04-08
    相关资源
    最近更新 更多