SpringContext etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
SpringContext etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

7 Eylül 2023 Perşembe

SpringContext @Qualifier Anotasyonu - Custom Annotation

Giriş
Açıklaması şöyle
While @Qualifier is helpful, using string identifiers can be error-prone. To avoid this, custom qualifiers come into play. These are annotations you create, annotated with @Qualifier. This gives you the advantage of compile-time checking, thereby reducing the possibility of errors.
Örnek
Elimizde iki tane anotasyon olsun. Burada SportsVehicle ve LuxuryVehicle diye iki tane custom annotation tanımlanıyor. 
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER,
  ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Qualifier
public @interface SportsVehicle {
}

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER,
  ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Qualifier
public @interface LuxuryVehicle {
}
Şöyle yaparız. Daha sonra her bean bunlardan hangisini kullanacaksa onunla işaretleniyor. @Autowired ile de hangi bean tipini istediğimizi belirtiyoruz
@Component
@SportsVehicle
public class Bike implements Vehicle {
 // …
}

@Component
@LuxuryVehicle
public class Car implements Vehicle {
 // …
}

@Autowired
@SportsVehicle
private Vehicle vehicle;


@Autowired
@LuxuryVehicle
private Vehicle vehicle;
Örnek
Elimizde kendi anotasyonumuz olsun. Bu anotasyonda @Qualifier kullanılıyor.
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE,
 ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Foo {
}
Bu anotasyonu kullanan bir metodumuz olsun.
@Foo
public IFooService service1() { return new SpecialFooServiceImpl(); }
Ya da bu anotasyonu direkt bean üzerinde kullanalım.
@Foo
@Component
public class EvenMoreSpecialFooServiceImpl { ... }
Bu anotasyonlara sahip bean'lere erişmek için şöyle yaparız. Burada List<Object> kullanılmasının sebebi bean'lerimiz arasında ortak bir ata sınıf olmaması
@Autowired
@Foo
List<Object> fooBeans; 
Eğer belli bir ata sınıftan kalıtan ve @Foo anotasyonuna sahip bean'leri istersek şöyle yaparız.
@Autowired
@Foo
List<SpecialFooServiceImpl> fooBeans;

@Value Anotasyonu - Ortam Değişkeni Okuma

Örnek
Şöyle yaparız
@Value("${env.SOME_ENV_VARIABLE:defaultValue}")
private String someEnvVariable;

SpringContext @Value Anotasyonu - Map Okuma

Örnek
application.properties dosyası şöyle olsun.
app.mappings={key1:'value1', key2:'value2'}
Şöyle yaparız.
@Value("#{'${app.mappings}")
private Map<String,String> mappings;
Örnek
application.properties dosyası şöyle olsun.
employee.age={one:'26', two : '34', three : '32', four: '25'}
Şöyle yaparız.
@Value("#{'${employee.age}")
private Map<String,Integer> employeeName;
Belli bir key değerine erişmek için şöyle yaparız.
@Value("#{'${employee.age}.two")
private String employeeName;
Belli bir value değerine erişmek için şöyle yaparız. Eğer value yoksa varsayılan değer olarak 30 gelir.
@Value("#{'${employee.age}.['two'] ?: 30")
private Integer employeeAge;

SpringContext @Value Anotasyonu - List Okuma

Giriş

Örnek
Elimizde şöyle bir kod olsun.
app.roles=ADMIN,USER,GUEST
Şöyle yaparız. Burada Spring Expression Language kullanılıyor.
"#{'${app.role}'.split(',')}"
private String[] role;
Örnek
Elimizde şöyle bir kod olsun.
prefix.list= some,stuff,with,comma,separators
Şöyle yaparız. Burada Spring Expression Language kullanılıyor.
"#{'${prefix.list}'.split(',')}"
private String[] array;
Örnek - Split İşlemi
application.properties dosyası şöyle olsun.
employee.names=Petey Cruiser,Anna Sthesia,Paul Molive,Buck Kinnear
Şöyle yaparız.
@Value("#{'${employee.names}'.split(',')}")
private List<String> employeeName;
Eğer sadece ilk elemana erişmek istersek şöyle yaparız.
@Value("#{'${employee.names}'.split(',')[0]}")
private String employeeName;
Örnek

application.properties şöyle olsun.
video.output.directory = C:\\Users\\Alex\\Desktop\\output

# Target Sites
target.sites[0] = https://youtube.com
target.sites[1] = https://bing.com
Şöyle yaparız.
@Configuration
public class WatchConfiguration {
  @Value("${video.output.directory}")
  private String videoDirectory;

  @Value("${target.sites}")
  private List<String> targetSites;
  ...
}