20 Ocak 2022 Perşembe

Jakarta EE @Qualifier Anotasyonu

Giriş
Açıklaması şöyle. Yani seçilebilecek iki bean arasından hangisini istediğimizi belirtiriz.
The @Qualifier annotation can be applied to another annotation for creating a custom qualifier:
...
In contrast to Spring’s @Qualifier, the Jakarta EE @Qualifier does not possess a String value attribute and hence, cannot be applied directly to bean definitions. Custom qualifiers can be further refined with additional attributes. An example for such additional attributes is the built-in @Named qualifier, which can be used both in Spring and Jakarta EE.
Açıklaması şöyle
If we don't have @Qualifier set, CDI won't know the default implementation and will throw the AmbiguousResolutionException. You can solve this problem using the Named annotation or a Qualifier. In our case, we will use the Qualifiers.
Örnek - Custom Qualifier
Şöyle yaparız
// custom qualifier definition:

@Qualifier
@Retention(RUNTIME)
@Target({ TYPE, METHOD, FIELD, PARAMETER })
public @interface Feline { }

// bean definition:

@Feline
class Cat implements Pet { }

// injection point:

@Inject
@Feline
Pet felinePet;
Örnek - Custom Qualifier
Elimizde şöyle bir kod olsun
public interface Instrument {
String sound(); } public enum InstrumentType { STRING, PERCUSSION, KEYBOARD; } @Qualifier @Retention(RUNTIME) @Target({TYPE, METHOD, FIELD, PARAMETER}) public @interface MusicalInstrument { InstrumentType value(); }
Farklı sınıfları tanımlayan şöyle bir kod olsun
@MusicalInstrument(InstrumentType.KEYBOARD)
@Default
class Piano implements Instrument {
  ...
}

@MusicalInstrument(InstrumentType.STRING)
class Violin implements Instrument {
...
}

@MusicalInstrument(InstrumentType.PERCUSSION)
class Xylophone implements Instrument {
  ...
}
Şöyle yaparız
@ApplicationScoped
public class Orchestra {

  @Inject
  @MusicalInstrument(InstrumentType.PERCUSSION)
  private Instrument percussion;
  @Inject
  @MusicalInstrument(InstrumentType.KEYBOARD)
  private Instrument keyboard;

  @Inject
  @MusicalInstrument(InstrumentType.STRING)
  private Instrument string;

  @Inject
  private Instrument solo;

  @Inject
  @Any
  private Instance<Instrument> instruments;

}
Default Qualifier
Açıklaması şöyle
In the absence of a qualifier, the built-in @Default qualifier is assumed both for bean definition and injection point. So the following, simple example without explicit qualifiers

// bean definition without explicit qualifier:

class Cat implements Pet { }

// injection point without explicit qualifier

@Inject
Pet felinePet;

is equivalent to the following example with an explicit @Default qualifier:
// bean definition with @Default made explicit:

@Default
class Cat implements Pet { }

// injection point with @Default made explicit:

@Inject
@Default
Pet felinePet;






Hiç yorum yok:

Yorum Gönder

Bean Validation @GroupSequence Anotasyonu

Örnek Elimizde şöyle bir kod olsun public class SampleRequest {   @NotNull   LocalDate startDate;   @NotNull   LocalDate endDate;   @AssertT...