autowired spring中初始化异常

本文探讨了SpringBean在不同工程目录下初始化异常的问题,详细分析了由于spring-core版本差异导致的构造函数@Autowired注解需求变化,并提供了有效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring bean初始化异常

相同的封装在不同的工程目录下,工程A可以正常运行,工程B包bean初始化异常。
异常信息

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'KLOrderService' defined in URL [jar:file:/usr/task/voyageone/script/oms/lib/ecerp-interfaces-koala.jar!/com/voyageone/ecerp/interfaces/third/koala/v2/KLOrderService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.voyageone.ecerp.interfaces.third.koala.v2.KLOrderService]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.voyageone.ecerp.interfaces.third.koala.v2.KLOrderService.<init>()
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105)

推测信息:spring-core的不同版本导致构造函数没有@Autowired下,表现行为不同,所以查询项目下的spring-core的版本。
不报错的版本管理如下:
在这里插入图片描述
报错的maven 管理如下:
在这里插入图片描述
果然,版本不同,在构造函数中通过@Autorwired来注入的话,只有在版本大于等于4.3才可以不用,否则的话spring 在初始化的时候会报错

Starting with Spring 4.3, if a class, which is configured as a Spring bean, has only one constructor, the @Autowired annotation can be omitted and Spring will use that constructor and inject all necessary dependencies.

Regarding the default constructor: You either need the default constructor, a constructor with the @Autowired annotation when you have multiple constructors, or only one constructor in your class with or without the @Autowired annotation.

解决方法

在构造函数上加入@autowired

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值