Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 3.2.1
    • Fix Version/s: Waiting for Triage
    • Component/s: Core:DI
    • Labels:
      None

      Description

      I believe that situation that I have described in Stackoverflow is a bug (see Reference URL). Such usage of @Autowired should work.

      In this case A object requires a B to be constructed, but B does not require anything. So the order could be:

      • Construct B
      • Construct A (using B)
      • set A for B

      And there is no circular dependency.


      Let me copy it here so that you don't have to jump to Stackoverflow:

      @Component
      private static class A {
          @Autowired
          public A(B b) {
          }
      }
       
      @Component
      private static class B {
          private A a;
       
          public B() {
          }
       
          @Autowired
          public void setA(A a) {
              this.a = a;
          }
      }

      Exception that I get:

      Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dependencyTest.A' defined in file [/home/adam/workspaces/testproject/target/classes/mypackage/test/DependencyTest$A.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [mypackage.test.DependencyTest$B]: : Error creating bean with name 'dependencyTest.B': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void mypackage.test.DependencyTest$B.setA(mypackage.test.DependencyTest$A); nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'dependencyTest.A': Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dependencyTest.B': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void mypackage.test.DependencyTest$B.setA(mypackage.test.DependencyTest$A); nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'dependencyTest.A': Requested bean is currently in creation: Is there an unresolvable circular reference?
          at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:730)
          at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:196)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1049)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
          at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
          at mypackage.test.DependencyTest.main(DependencyTest.java:10)
      Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dependencyTest.B': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void mypackage.test.DependencyTest$B.setA(mypackage.test.DependencyTest$A); nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'dependencyTest.A': Requested bean is currently in creation: Is there an unresolvable circular reference?
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:891)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:834)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:749)
          at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:795)
          at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723)
          ... 14 more
      Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void mypackage.test.DependencyTest$B.setA(mypackage.test.DependencyTest$A); nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'dependencyTest.A': Requested bean is currently in creation: Is there an unresolvable circular reference?
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:601)
          at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
          ... 26 more
      Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'dependencyTest.A': Requested bean is currently in creation: Is there an unresolvable circular reference?
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:327)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:217)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:891)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:834)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:749)
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:558)
          ... 28 more

        Issue Links

          Activity

          Hide
          apierzch Adam Pierzchała added a comment -

          What is more, there is inconsistency. A reversed case does work:

              @Component
              private static class A {
           
                  public A() {
                  }
           
                  @Autowired
                  public void setB(B b) {
                  }
              }
           
              @Component
              private static class B {
                  public B() {
                  }
              }

          And there is a workaround for the first example that I have provided (adding @DependsOn to A class) but when there are more than two classes involved it is hard to determine which ones should have @DependsOn.

          Show
          apierzch Adam Pierzchała added a comment - What is more, there is inconsistency. A reversed case does work: @Component private static class A {   public A() { }   @Autowired public void setB(B b) { } }   @Component private static class B { public B() { } } And there is a workaround for the first example that I have provided (adding @DependsOn to A class) but when there are more than two classes involved it is hard to determine which ones should have @DependsOn.
          Hide
          apierzch Adam Pierzchała added a comment -

          It looks like related to SPR-1487.

          Show
          apierzch Adam Pierzchała added a comment - It looks like related to SPR-1487 .
          Hide
          shpikachu Alexei Shpikat added a comment - - edited

          Looks like a duplicate for SPR-8704 and SPR-9255

          Show
          shpikachu Alexei Shpikat added a comment - - edited Looks like a duplicate for SPR-8704 and SPR-9255

            People

            • Assignee:
              juergen.hoeller Juergen Hoeller
              Reporter:
              apierzch Adam Pierzchała
              Last updater:
              Juergen Hoeller
            • Votes:
              3 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Days since last comment:
                4 years, 12 weeks, 2 days ago