In the absence of DatabaseWriter, I'm currently using getBeanNamesForType method to inject the only available bean. But when I have multiple implementations of the interface, I'm not sure how to do this effectively. Using getBeanNamesForType and iterating over the retrieved list doesn't really look like an elegant way. I want to avoid hard-coding the bean names inside the someMethod(). I'm sure this must be a very common use-case today. What's the standard way of solving this using spring?
Swapnil S. Sonawane<br /> <br />B.Tech (Expected May 2009)<br />Computer Science, NIT Durgapur, India<br />SCJA 1.0
But isn't it that I will have two matching beans for the interface type defined in the class which is going to make use of it eventually. I mean there will be two beans compatible with the interface type, which throws UnsatisfiedDependencyException according to the documentation. Please correct me if I'm wrong.
Yes, swapnil you are right, autowire byType allows a property to be autowired if exactly one bean of the property type exists in the container. If more than one exists, a fatal exception is thrown, which indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens; the property is not set.
subject: Using dependency injection for instantiating multiple implementations for an interface