Reputation: 61
What's the different between using a qualifier and just specifying the exact implementation class you want?
This question is copied from: http://docs.jboss.org/weld/reference/latest/en-US/html/injection.html
Upvotes: 3
Views: 610
Reputation: 14636
Generally speaking, Qualifiers form an layer of decoupling between parties. This allows you to change the implementation of one side without changing the other.
However, I find it very rare to actually have more than one implementation class.
This depends on the usecase. While you are probably right for most "inner" classes of your business logic (there will be one MailService
and not two or three), there usually are some beantypes that have more than one implementation.
@Inject @LDAP
private AuthenticatorService
vs
@Inject @FormBased
private AuthenticatorService
Mocking / providing different beans for different deployment scenarios has already been mentioned.
Also, you will almost certainly have some kind of logic like this, where qualifiers tell "a part of the story" and make the code readable and structured:
@Inject @Authenticated
private User user;
Upvotes: 1
Reputation: 33171
Functionally, they're the same. However, I find it very rare to actually have more than one implementation class. You'll find dependency injection to be much more useful when you're unit testing your classes. Dependency Injection allows you to easily inject a regular implementation instance during run-time. Then in your unit tests, when you're manually constructing your object to test, you can easily pass in a mock implementation.
Upvotes: 0