This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Spring and the fly likes Parent and child relationship configuration in spring? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Parent and child relationship configuration in spring?" Watch "Parent and child relationship configuration in spring?" New topic

Parent and child relationship configuration in spring?

scott miles
Ranch Hand

Joined: Jun 16, 2011
Posts: 70
Say we have following class definition

public Class MyClass1{

private ParentClass1 parentInstance;

public MyClass1(ParentClass1 instance)


Now we have five child class which extends ParentClass1. Now in spring when are composing MyClass1 ,how we will determine which child instance out of five needs to be injected here (as it depends upon some runtime parameter depending on which we have to create child instance). Please help me here how we create the configuration file i.e spring-config.xml for above scenario(i mean is there any way to configure such type of scenarios in spring)?
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
This simple enough if you know how to create and define bean definition in your configuration file. Simply you just define all child class that you have and inject in the parent bean that you created with i.e "<constructor-arg ref="childClass1"/>".

From "MyClass1" you need to declare all child class reference variable and pass all ref variable in the constructor public MyClass1() similar what you already did.

Start from there and show us what you've done, I'll walk you through from there. Feel free post any question that you might have...

"Your life is in your hands, to make of it what you choose."
I agree. Here's the link:
subject: Parent and child relationship configuration in spring?
It's not a secret anymore!