Let's say I have this class
class Foo implements IFoo { Foo() {} }
class Fooz implements IFoo { Fooz() {}}
class Foobar implement IFoobar {
@Autowired
Foobar (Foo foo) {}
}
class Foobarz implement IFoobar {
@Autowired
Foobarz (Bar bar) {}
}
In asimple case I can do to solve my problem:
class Bar {
@Autowired
Bar (IFoo foo) {
this.foo = foo;
}
}
However if I want to be able to select my IFoo and IFoobar instance according to my configuration file I need to do do:
@Configuration
class Configuration {
@Bean
foo () {
return this.isZ() ? new Fooz() : new Foo ();
}
@Bean
foobar () {
return this.isZ() ? new Foobarz(/* ??????? */) : new Foobar (/* ??????? */);
}
}
As you can see I can't instantiate my Foobar, since I need another bean. I know there is the ApplicationContext.getBean, but I can't be sure it will be indeed initialized in my Configuration
class when foobar()
gets called.
And I don't want to call this.foo()
either, because that would create another reference of the object, and I'm not sure about the order of execution and initialization