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 Inheritance Tree Into List With Spring

Load Inheritance Tree Into List With Spring

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

Join the DZone community and get the full member experience.

Join For Free

i noticed interesting spring feature. one of my colleagues used it for loading whole inheritance tree of spring beans into list. missed that when i was studying spring docs.

let's have this inheritance tree of spring beans:

animals-class-diagram

in following snippet is this tree of beans loaded into list with constructor injection:

@component
public class nature {
	list<animal> animals;

	@autowired
	public nature(list<animal> animals) {
		this.animals = animals;
	}

	public void showanimals() {
		animals.foreach(animal -> system.out.println(animal));
	}
}

method showanimals is using java 8 lambda expression to output loaded beans into console. you would find a lot of reading about this new java 8 feature 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.showanimals();
	}
}

console output:

polarbear []
wolf []
animal []
grizzly []
bear []

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

Spring Framework Tree (data structure) Inheritance (object-oriented programming)

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

  • Best Practices for Resource Management in PrestoDB
  • 6 Best Books to Learn Multithreading and Concurrency in Java
  • A First Look at CSS When and Else Statements
  • Everything I Needed to Know About Observability, I Learned from ‘Bewitched’

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