Repository navigation
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
Activity
spring-projects-issues commentedon Oct 20, 2009
Denis Zhdanov commented
Sorry, didn't define priority for the ticket (it is 'Major' by default). Suggest to change it to 'Minor'
spring-projects-issues commentedon Oct 20, 2009
Denis Zhdanov commented
One more note - ticket text says about 'Ordered' annotation - should be replaced to 'Order' (the links are correct).
spring-projects-issues commentedon Sep 10, 2010
mck commented
I haven't checked your patch, but i reckon the third approach should only work on SortedSet and SortedMap.
If autowired can already work on SortedSet and SortedMap
wouldn't this already implement this feature?
spring-projects-issues commentedon Sep 11, 2010
Denis Zhdanov commented
The point is to allow to define sorting rule via
@Ordered. I.e. there is a possible situation that I have a number of different classes that, say, conform to the same interface and don't know anything about each other. I don't want to explicitly push that knowledge by introducing compareTo() to them. I want just configure fine-grained autowiring rules at the very similar way to standard Spring facilities.spring-projects-issues commentedon Sep 12, 2010
mck commented
Yes i understand and value both points one and two (and your last comment). They certainly are a required addition to Spring imho and already have my vote.
My questioning was solely around your third point:
"if both classes of compared elements are not marked by Ordered but implement Comparable with consisting type parameter"
I didn't understand the need for this because it already works in Spring.
For example ```
class AbcService implements Comparable<AbcService>{...}
class MyClass{
@AutowiredSortedSet<AbcService> abcServices;
}```
already works in Spring and does not need the code you've added in AutowiredElementsComparator.compare(...)
But i see here you're actually talking about the possibility to blend
@Orderand Comparable in the one sorting process? This sounds a bit scary to me... would people be expecting Autowired Lists to be sorted by their natural order (since that is what SortedSet is used for), and how would this effect backward-compatibility? If it were to be done, so that "@Orderfell back onto natural sorting", then i would expect that the sorting between two identical@Ordervalues also fell back to the natural sorting if available (here this would mean changing AutowiredElementsComparator:47 to ```if (a1 != null && a1.value() != a2.value()) {
spring-projects-issues commentedon Sep 12, 2010
Denis Zhdanov commented
Oh, I see your point now.
My idea was to allow to define ordering during autowiring mixed collections, i.e. collections that consist from both
@Orderedor non-@Orderedobjects.Agreed that it looks more consistent to fall back to natural ordering in case of equal
@Order-impliedvalues.Unfortunately, it looks like Spring guys don't pay to much attention to this patch.
spring-projects-issues commentedon Oct 26, 2010
David Erickson commented
I would love to see this feature as well, I was a little disappointed it wasn't already implemented given everything else I can dream up already works :)
spring-projects-issues commentedon Jul 7, 2011
Chris Beams commented
Duplicates #10245. Please watch that issue to be notified of changes.