Skip to content

Allow to define autowired collections and array elements order [SPR-6253] #10921

@spring-projects-issues

Description

@spring-projects-issues

Denis Zhdanov opened SPR-6253 and commented

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).


Affects: 3.0 RC1

Attachments:

Issue Links:

8 votes, 6 watchers

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)status: duplicateA duplicate of another issuetype: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions