Member-only story

Spring Autowired Collections, Lambda Functions, and Java Streams

Suresh Kandula
4 min readMay 10, 2020

--

You might be thinking, how I would connect these seemingly distinct concepts into a single discussion. So, read on…

The genesis of this is really from a code review I was conducting with a colleague of mine, into the wee hours of a weekday. Yes, we still do those around here…

Photo by Bogdan Karlenko on Unsplash

Some facts first, nothing fancy:

  • If you Autowire an iterable collection (e.g., List), Spring (4+) will create an instance of every interface implementation or sub-class of the “Runner” in the example below. If you have 3 implementations of this interface in this namespace, the size of this list is 3, when you refer to this attribute.
  • If this interface happens to be a Functional Interface, you essentially have multiple Lambda function implementations.
  • You could stream an iterable and dynamically execute lambda functions on each item in the collection.
  • Annotating with @Order will give a user sorted list during Autowiring a collection, this annotation is available since Spring 2

Now, you might be wondering, where are we going with this? So, let me cut to the chase. I was hoping to implement a few design patterns elegantly with these Spring features.

--

--

Suresh Kandula
Suresh Kandula

Written by Suresh Kandula

#FinancialServices #Automotive #Architecture #LoveOfCoding

No responses yet