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

Allow to define autowired collections and array elements order

    Details

    • Type: New Feature
    • Status: Resolved
    • Priority: Major
    • Resolution: Duplicate
    • Affects Version/s: 3.0 RC1
    • Fix Version/s: None
    • Component/s: Core
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      Spring defines a contract for autowiring arrays and type collections (3.4.5 Autowiring collaborators, 3.9.2 @Autowired and @Inject).

      However, there is a possible use-case that a user wants to define particular order for autowired collection/array. It looks obvious in the case of autowiring typed lists (the client may want to preserve particular order for the list elements) but the same idea may be spread easily to sets/maps/arrays.

      I suggest the following rules to be used during examining the order for two elements:

      • if both classes of compared object are marked by Ordered the one with lower value is considered to be less than another;
      • if class of one element is marked by Ordered and class of another is not marked by Ordered the former is considered to be less than the later;
      • if both classes of compared elements are not marked by Ordered but implement Comparable with consisting type parameter they are compared via Comparable#compareTo(Object);

      Please note that the rules above may be applied to all elements of autowired collection/array in order to define particular for them.

      Implementation

      That feature may be optional and defaults to perform ordering. It seems that most natural to allow to define it via additional method of @Autowired annotation.

      So, it can be implemented easily via minor AutowiredAnnotationBeanPostProcessor modification - the argument to be injected is just sorted if necessary.

      Attached archive contains either feature implementation (classes from 'org.springframework.beans.factory.annotation' package) or usage example (classes from 'com.spring.example' package and its subpackages).

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                cbeams Chris Beams
                Reporter:
                denis.zhdanov Denis Zhdanov
                Last updater:
                Trevor Marshall
              • Votes:
                8 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Days since last comment:
                  6 years, 42 weeks, 4 days ago

                  Time Tracking

                  Estimated:
                  Original Estimate - 10d
                  10d
                  Remaining:
                  Remaining Estimate - 10d
                  10d
                  Logged:
                  Time Spent - Not Specified
                  Not Specified