0

I have class structure that look like below:

class Base {}

class ChildA extends Base {}

class ChildB extends Base {}

I got this in my application code:

Base theObj = null;
if( str.equalsIgnoreCase("object_A") == true ) {
   theObj = new ChildA();
} else {
   theObj = new ChildB();
}

May I know how to configure the bean in Spring?

huahsin68
  • 6,819
  • 20
  • 79
  • 113

1 Answers1

0

One option is to create refs for two different beans (childA, childB) in spring xml context. And then in creation for main bean specify constructor with args. You can implement constructor with similar code as you mentioned.

mishadoff
  • 10,719
  • 2
  • 33
  • 55
  • What show in my mind is to use either AbstractFactoryBean because it contain a logic selection on which child bean to be create during runtime. – huahsin68 Jun 23 '12 at 07:21