A
A
Anrek2021-09-08 19:33:13
Android
Anrek, 2021-09-08 19:33:13

Why is @Qualifier needed if there is nothing after it?

Good evening!

There is a Kotlin file that declares many different annotations in a list (the first two are for example):

import javax.inject.Qualifier

@Qualifier
annotation class WithErrorHandler

@Qualifier
annotation class UseCaseScope
..

What I read about the annotation: "The @Qualifier annotation allows you to specify the name of the bean to be injected. It is used right before the argument."

But after all, after the annotation, there are no brackets in which something would be specified, for example, like this:
@Qualifier("appContext1")

Question: why might the @Qualifier annotation be needed here if there is nothing after it? What do I understand wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anrek, 2021-09-11
@Anrek

I figured it out. There are @Qualifier from different packages. Both of them are about DI (dependency injection).
In real life, a situation may arise when several beans of the same type are created (for example, with 1 and the same interface), but in a specific case, for example, a specific bean is needed. @Qualifier - the annotation allows you to specify which one.
1) There is a @Qualifier that is imported from the CDI package JSR 330 (CDI is the Java standard for contexts and dependency injection, and JSR-330 is a subset of it that it fully supports), imported like this:
import javax.inject.Qualifier
Its qualification is not added, just written like this:
@Qualifier
2) There is a @Qualifier that is imported from the Spring framework package (a dependency injection framework), imported like this:

import org.springframework.beans.factory.annotation.Qualifier

And it just indicates the "clarification" in brackets:
@Qualifier("appContext1")
In Spring, the bean is called by name and is called explicitly by it.
In the case of JSR 330, this creates its own annotation to separate dependencies with the same interfaces and then applies to both the bean class and the dependency. The document about which there was a speech in a question just the list of such summaries.
In summary: in Spring, we define the required bean by its name, and in JSR 330, by the name of its annotation, and it is convenient to collect a list of all these annotations in a separate file (which we did).
Source (see examples here)

J
Jacen11, 2021-09-09
@Jacen11

What do I understand wrong?

apparently all
you indicate is that the annotation class UseCaseScope will specify which bean. You put it where the bean is created and where to insert it. Usually, one object of the same type is created and injected, and then it’s clear where to insert it. And this is when there are many beans of the same type.
But after all, after the annotation, there are no brackets in which something would be specified, for example:
@Qualifier("appContext1")

what it should mean and what I was going to clarify, I did not understand

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question