DZone
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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Smart Dependency Injection With Spring: Assignability (Part 2 of 3)
  • Spring Beans With Auto-Generated Implementations: How-To
  • Beyond SOLID: Embracing CUPID for Modern Software Craftsmanship
  • Beyond Conversation: Mastering Context with Claude Code Skills and Agents

Trending

  • Production-Grade RAG: Why Vector Search Isn't Enough (and How Hybrid Search Fills the Gaps)
  • AI Paradigm Shift: Analytics Without SQL
  • What Is Plagiarism? How to Avoid It and Cite Sources
  • AI Agents in Java: Architecting Intelligent Health Data Systems
  1. DZone
  2. Coding
  3. Frameworks
  4. Load All Implementors of an Interface into List with Spring

Load All Implementors of an Interface into List with Spring

By 
Lubos Krnac user avatar
Lubos Krnac
·
May. 20, 14 · Interview
Likes (3)
Comment
Save
Tweet
Share
61.4K 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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Smart Dependency Injection With Spring: Assignability (Part 2 of 3)
  • Spring Beans With Auto-Generated Implementations: How-To
  • Beyond SOLID: Embracing CUPID for Modern Software Craftsmanship
  • Beyond Conversation: Mastering Context with Claude Code Skills and Agents

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook