posted 13 years ago
There are many solutions and also it depends.
In the basics of xml, the config file is about static configuration of stuff you know up front.
For more dynamic runtime requirements you have some choices.
1) If you are using Spring 3.x there is the Spring Expression Language that you can use, but the dependency is still set once up front, so once you choice of the fve is determined that object will always reference the one that you choice.
2) If you are not using Spring 3.x and you still are ok with the dependency set up front once, you could use the PropertyPlaceholderConfigurer and create a .properties file with the value you want for that particular run. This is still a little bit static and needs to be known up front, but you are externalizing the choice from your xml.
3) If it is more dynamic where the dependency can change often, then you might go the approach of creating a Factory POJO class that has a factoryMethod that takes a parameter and based on that value the factory returns one of the five instances. Then define the Factory class as a bean and inject the factory into your class, and when you need a dependency then in code call the factory method passing in the value for your int i.
4) You could create a map as a bean with all five instances of the subclass in it with a key of your int i, then inject the map into your class and when you need your child class get it from the Map.
Those are a few choices.
Hope that helps.
Mark