Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘commonConvertServiceImpl’: Unsatisfied dependency expressed through field ‘****Service’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘****ServiceImpl’: Unsatisfied dependency expressed through field ‘****Service’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘****ServiceImpl’: Unsatisfied dependency expressed through field ‘iConvertService’; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘****ServiceImpl’: Bean with name ‘***ertServiceImpl’ has been injected into other beans [***ServiceImpl,***ManagerServiceImpl] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using ‘getBeanNamesForType’ with the ‘allowEagerInit’ flag turned off, for example.
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessorCaused by: org.springframework.beans.factory.UnsatisfiedDependencyException_javadoGetBeanCaused by: org.springframework.beans.factory.UnsatisfiedDependencyException_spring_02AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
… 57 common frames omitted

如上错误,我们可以看到主要问题是循环依赖导致项目产生错误。

也就是这个iConvertService@Autowired修饰的下造成的循环依赖,我们可以用如下解决办法:

  • 构造函数注入改为Setter方法注入:将循环依赖的类之间的构造函数注入改为Setter方法注入。这样可以打破循环依赖链条,使得Spring能够正确地解析依赖关系。
  • 使用@Lazy注解延迟初始化:在循环依赖的其中一个类上使用@Lazy注解,将其标记为延迟初始化。这样Spring会在需要时才创建对象,从而避免循环依赖问题。
  • 使用@DependsOn注解指定初始化顺序:使用@DependsOn注解来指定Bean的初始化顺序。通过在循环依赖的类上使用@DependsOn注解,可以明确指定它们之间的初始化顺序,从而解决循环依赖问题。
  • 使用代理对象:在循环依赖的类中使用代理对象来解决循环依赖。Spring可以通过代理对象来解决循环依赖问题,从而保持依赖关系的正确性。
  • 使用@Autowired注解指定特定的Bean名称:在循环依赖的类中使用@Autowired注解,并通过@Qualifier注解指定特定的Bean名称。这样可以明确指定依赖的Bean,从而解决循环依赖问题。
    而我选择的办法则是:在造成循环依赖的类里边调用的iConvertService 接口上加上@Lazy。
@Autowired
@Lazy
private IConvertService iConvertService;

进而解决相关问题。