M
M
MisterParser2019-01-26 14:21:15
Java
MisterParser, 2019-01-26 14:21:15

What is the difference between @Qualifier annotation and interface?

I'm reading the book Spring in action and the question arose why use qualifiers (in the book it shows on the example of string instruments), if you can simply implement the marker interface?
Interested in the pros and cons of the two methods, this is not described in the book.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan Lopatin, 2019-01-26
@MisterParser

`@Qualifier` allows you to make multiple implementations of the same interface.
A marker interface is not always an option. For example, if you need multiple database connections, then you need the `DataSource` interface for all of them. And to inherit from its implementations in order to add this very marker interface is not even always possible. For example, if the DataSource is implemented somewhere deep inside a connection pool or a JDBC driver.

B
BorLaze, 2019-01-26
@BorLaze

No need to produce entities unnecessarily.
If we do everything through interfaces, then for three, say, implementations, we need 4 files - a base interface with methods, and three heirs that will act as a selector.
Through @Qualifier all this is not necessary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question