Java annotation for url - what is use case?

I am trying to figure out what the annotation generated in the code I have inherited does.

Here is the definition of annotation for code:

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import javax.inject.Qualifier;

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface BaseUrl {
}

      

and then using dagger 2 (android dependency injection framework) it is used like this:

//from a file called applicationModule.java
@Provides
    @Singleton
    Endpoint provideBaseURL(@BaseUrl String url) {
        return getEndPointUrl(url);
    }

      

I'm more worried about the @BaseUrl annotation. what does he do in this context?

The project uses Retrofit.

+3


source to share


1 answer


I finally found what's going on and I would like to share. The dagger has two ways to help when one type cannot identify the supplier. Either use the @named or @Qualifier annotation. The @named annotation can be used by default, but if you want to create your own annotation to identify the provider, you can use @Qualifier.



So in example i, assuming @BaseURL is nothing more than an @Named tag to attach to the provider. They do the same thing, but instead of using the "Named" word that is provided by the dagger, you can make your own. The docs are here in the qualifier section.

0


source







All Articles