Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-9685

Method-based autowiring fails in certain contexts

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Minor
    • Resolution: Won't Fix
    • Affects Version/s: 3.1.2
    • Fix Version/s: None
    • Component/s: Core:DI
    • Last commented by a User:
      true

      Description

      If an application context contains more than one instance of a bean whose class has an @Autowired setter method, then it's possible that some of the beans' dependencies do not get injected.

      The attached TestNG unit test demonstrates the bug. It creates two singletons of type AutowiredMethodBean...one from XML and the other from a @Configuration. When the bean defined in the XML is initialized first, then the JavaConfig's bean will not be autowired and the test will fail. The order in which the beans are instantiated is crucial...the test passes if the JavaConfig bean is created first.

      This problem looks to be due to the cached metadata in AutowiredAnnotationBeanPostProcessor. If the metadata is lazily created when initializing the XML bean with its explicit property declaration, then the field is marked as skipped and injection won't be attempted on the JavaConfig bean.

      This unit test uses a mixed XML/JavaConfig application context because that's the configuration of the application I was working on when I discovered the issue. I suspect that it's possible to manifest with XML-only configuration if the first bean has the property explicitly set and the second uses autowiring.

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            stevevls Steve van Loben Sels
            Last updater:
            Spring Issues Spring Issues
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Days since last comment:
              3 years, 23 weeks, 5 days ago