DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Load All Implementors of an Interface into List with Spring

Load All Implementors of an Interface into List with Spring

Lubos Krnac user avatar by
Lubos Krnac
·
May. 20, 14 · Java Zone · Interview
Like (0)
Save
Tweet
53.82K Views

Join the DZone community and get the full member experience.

Join For Free

last week i wrote a blog post how to load complete inheritance tree of spring beans into list . a similar feature can be used for autowiring all implementors of a certain interface.

let’s have this structure of spring beans. notice that bear is abstract class, therefore it’s not a spring bean. so we have three beas: wolf, polarbear and grizzly.

implementors

in following snippet are all implementors loaded into list with constructor injection:

@service
public class nature {
	list<runner> runners;

	@autowired
	public nature(list<runner> runners) {
		this.runners = runners;
	}

	public void showrunners() {
		runners.foreach(system.out::println);
	}
}

method showrunners is using java 8 foreach method that consumes method reference.  this construct outputs loaded beans into console. you would find a lot of reading about these new java 8 features these days.

spring context is loaded by this main class:

public class main {
	public static void main(string[] args) {
		annotationconfigapplicationcontext context =
			new annotationconfigapplicationcontext(springcontext.class);
		nature nature = context.getbean(nature.class);
		nature.showrunners();
	}
}

console output:

polarbear []
wolf []
grizzly []

this feature can be handy sometimes. source code of this short example is on github .

Spring Framework Interface (computing)

Published at DZone with permission of Lubos Krnac, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Autowiring in Spring
  • OPC-UA, MQTT, and Apache Kafka: The Trinity of Data Streaming in IoT
  • Which JVM Version Is the Fastest?
  • Ultra-Fast Microservices: When Microstream Meets Payara

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo