Mark Spritzler wrote:There are many approaches to resolve ambiguity.
1) Use @Qualifier next to your @Autowired so you can tell which one to inject by name.
2) Put @Primary on one of the implementations, then Spring will inject that one.
3) Only have one of the two implementations in your classpath at runtime, so that there will ever only be one implementation.
For 3, I have yet to see any good examples of why you would need multiple implementations of a Service or a Repository or a Controller, EndPoint, or any other class that you write.
However, there are cases where you might have multiple DataSources, TransactionManagers, or other infrastructure beans. In which case, you only have option #1, because you can't put @Primary into a class you didn't write and only have the .class of.
Good Luck
Mark
I have an example of using multiple implementations of a Service.
I'm working on a project that need fake service implementations. I do not need mock at this case... Suppose I have a specific service which collect data from any database but I can't use this service when working at some new functionality that depends on this service. I'd like to use another implementation which returns fake data only to continue working.
Even considering this example, I think using Qualifier or Primary is not a better approach because I don't want that programmers need to change code but instead some kind of config file.
Any ideas?
thanks