java启动报错,应如何解决?

重置了一下idea编译器 产生以下问题

15:43:28.674 [main] ERROR o.s.b.SpringApplication - [reportFailure,837] - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'qiyeApiController': Unsatisfied dependency expressed through field 'qyglQiyeService'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'qyglQiyeServiceImpl': Bean with name 'qyglQiyeServiceImpl' has been injected into other beans [whpChuanganqiServiceImpl] 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.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1420)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at com.yaodong.enterprisebase.YaoDongEnterpriseApplication.main(YaoDongEnterpriseApplication.java:30)
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'qyglQiyeServiceImpl': Bean with name 'qyglQiyeServiceImpl' has been injected into other beans [whpChuanganqiServiceImpl] 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.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:623)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1307)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
    ... 20 common frames omitted

重启等操作没用

阅读 1.4k
avatarAI BotBETA

您的错误是由于 Spring 中的循环依赖导致的,具体是 qyglQiyeServiceImplwhpChuanganqiServiceImpl 互相注入,造成了启动失败。以下是解决步骤:

解决方案

  1. 打破循环依赖(首选方法)

    • 重构代码,避免直接注入服务之间的相互依赖。例如:

      • whpChuanganqiServiceImplqyglQiyeServiceImpl 的注入点上,改为使用 setter 注入而不是字段注入。
      • 或引入一个第三方的服务层(如 Facade 层)来协调逻辑。
    • 示例修改(在 whpChuanganqiServiceImpl.java 中):

      // 替换字段注入:
      @Autowired
      private QyglQiyeService qyglQiyeService;
      
      // 改为 setter 注入:
      private QyglQiyeService qyglQiyeService;
      
      @Autowired
      public void setQyglQiyeService(QyglQiyeService qyglQiyeService) {
          this.qyglQiyeService = qyglQiyeService;
      }
  2. 使用 @Lazy 注解延迟加载

    • 在其中一个服务的注入点上添加 @Lazy。例如,在 whpChuanganqiServiceImpl 注入 qyglQiyeServiceImpl 的地方:

      @Autowired
      @Lazy // 延迟加载,打破循环
      private QyglQiyeService qyglQiyeService;
    • 注意:这只是一个临时解决方案,长期推荐优先重构代码。
  3. 检查 Spring 配置

    • 确保在 Spring Boot 的主配置文件中启用循环依赖支持(仅在必要时代码):

      @SpringBootApplication
      public class YourApplication {
          public static void main(String[] args) {
              new SpringApplicationBuilder(YourApplication.class)
                  .allowCircularReferences(true) // 允许循环依赖
                  .run(args);
          }
      }
    • 或检查是否有配置文件(如 application.properties)设置了 spring.main.allow-circular-references=true

为什么这些方法有效

  • 循环依赖发生在两个服务相互引用时,Spring 无法在初始化阶段完全创建它们。重构代码(如用 setter 注入)能让 Spring 推迟依赖设置。
  • @Lazy 注解让注入点延迟加载,避免在启动时立即创建,从而打破循环。
  • 启用 allowCircularReferences 是备选方案,但不推荐生产使用,因为它可能导致其他问题(如代理错误)。

重启 IDEA 或系统没用,因为这需要代码层修复。检查是否有最近的代码变更(尤其是服务实现类),优先尝试步骤1或2。

1 个回答

File-Invalidate Caches 清一下idea缓存试试呢

推荐问题