@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对象({})。然而,我试图让repo映射对象由Spring生成。所以我把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)}
然而,当我重构帐户ID和地图键到字符串类型,我能够产生HashMap包含一个'假人'变量,这是奇怪的:
{account=com.doubleliu.model.Account@397fbdb}
回到Integer,我无法修复错误,然后我尝试将自动连线移到DataRepoImpl类的空构造函数:
@Autowired
public DataRepoImpl() {
}
然而,我从repo得到空值,因为(我的假设)对象尚未创建或分配给repo Map变量。然后,我再次将自动连线移动到以map为参数的构造函数上:
@Autowired
public DataRepoImpl(Map<Integer, Account> repo) {
this.repo = repo;
}
这次创建了repo对象({}),并且不为null,我假设该对象是通过参数创建的,这是使用spring创建它的正确方法吗?此外,我的IDE将我标记为错误无法自动连线。没有豆子的地图
我刚刚开始学习spring,试图在进入下一个阶段之前完全理解它。因此,任何对代码的响应、解决方案、注释、建议、修复和问题都非常满意,尤其是autowired。非常感谢。
编辑:我正在使用基于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"/>
感谢您的帮助和意见,欢迎您提出更多意见和建议。
问题内容: 我在Spring定义了这样的地图: 然后,我将该bean自动装配为定义为的属性: 这样做时,会抛出一个异常,说: `Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘DutyCreator’: Injection of autowired
问题内容: 需要一些帮助,我刚刚开始学习Spring,似乎无法弄清楚我们的错: Application.java-没有包 User.java-包com.mapping UserDAO.java-包com.accesors Root.java-包com.controllers 当我运行项目时,我似乎得到了以下启示 堆栈跟踪: 据我了解,这意味着@ComponentScan没有检测到软件包 问题答案:
我们正在使用Spring框架5和Spring Boot 2.0.0。M6,我们也在使用WebClient进行反应式编程。我们为我们的反应式Restendpoint创建了测试方法,所以我查找了一些关于如何做到这一点的例子。我发现这个或这个以及许多其他的都一样。他们只是自动绑定一个WebTestClient。所以我尝试了同样的方法: 我无法运行此操作,因为我收到错误信息: 因此,似乎不存在自动配置。我
在试图通过浏览器访问我的应用程序时,我遇到了不少错误。错误包括: 无法自动连线方法:public void com。ProjectOne。Util。自定义HibernatedAOSupport。anyMethodName(org.hibernate.SessionFactory);嵌套的异常是org。springframework。豆。工厂NoSuchBeanDefinitionException
我在spring中定义了一个映射: 然后我将这个bean自动转换为一个属性,该属性定义为: 干杯。
问题内容: 如果Service类使用Validated注释进行注释,则同一类无法自动装配自身。 这是在Spring Context尝试加载时引发的异常: 同样,当您有很多依赖于类的自身时,就会发生这种情况(当某个服务使用使用第一个服务的其他服务时)。我想知道@Validated注解,但是我总是在bean上遇到同样的错误。 有人知道我该怎么解决吗? 问题答案: 在这种情况下,注释与错误的自动装配无关