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