-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Description
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:
- ordered-autowired-collection.tar.gz (9.63 kB)
Issue Links:
- Autowiring should support ordered collection driven by Order annotation or Ordered interface [SPR-5574] #10245 Autowiring should support ordered collection driven by Order annotation or Ordered interface ("duplicates")
8 votes, 6 watchers