Giriş
@ConfigurationProperties Anotasyonu ile application.properties dosyasından Basit String, Liste ve daha bir sürü şey okumak mümkün. Bu yazıda bir POJO sınıfının nasıl okunacağı gösteriliyor.
Örnek - String Taşıyan POJO
application.properties şöyle
olsun.
section1:
key1: "value1"
key2: "value2"
section2:
key1: "value1"
key2: "value2"
Şöyle
yaparız
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "section1")
public class MyCustomSection1 {
private String key1;
private String key2;
// define setters and getters.
}
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "section2")
public class MyCustomSection1 {
private String key1;
private String key2;
// define setters and getters.
}
Örnek String Taşıyan POJO
application.properties şöyle
olsun.
test1.name=Ken
test2.name=Anthony
Elimizde şöyle bir POJO
olsun.
@Component
public class People {
private String name;
public String getName() {
return this.name;
}
}
Şöyle
yaparız
@SpringBootApplication
public class Application {
@Bean
@ConfigurationProperties(prefix="test1")
public People man1() {
return new People();
}
@Bean
@ConfigurationProperties(prefix="test2")
public People man2() {
return new People();
}
}
Örnek - String Taşıyan POJO Listesi
Elimizde şöyle bir yaml dosyasında
olsun.
cache:
config:
defaultConfigs:
- cacheName: ONE_HOUR_CACHE
timeToLiveSeconds: 3600
- cacheName: TEN_MINUTES_CACHE
timeToLiveSeconds: 600
Elimizde şöyle bir kod
olsun.
public class CacheSettingsModel {
private String cacheName;
private String timeToLiveSeconds;
...
}
Listesini okumak için şöyle
yaparız. Liste ismi ile yaml dosyasındaki alan ismi aynıdır.
@Component
@ConfigurationProperties( prefix="cache.config")
public class CacheSettings {
private List<CacheSettingsModel> defaultConfigs;
...
}
Örnek - String Taşıyan POJO Listesi
Elimizde application.yml dosyası
olsun.
rules:
- name: abc
value: something
- name: edf
value: something
Elimizde şöyle bir kod
olsun
public class Rule {
public String name, value;
}
Şöyle
yaparız.
@Configuration
@ConfigurationProperties(prefix="rules")
public class Rules {
public List<Rule> list;
}
Örnek - POJO Listesi Taşıyan POJOBu örnekte bir Converter kullanmak gerekiyor. Elimizde şöyle bir application.propterties
olsunfeature.toggles.showFormatted = false
feature.toggles.debug = true
Elimizde şöyle bir POJO
olsunpublic class FeatureToggle {
private final boolean value;
public FeatureToggle(boolean value) {
this.value = value;
}
public boolean isEnabled() {
return value;
}
public boolean isDisabled() {
return !value;
}
}
application.properties değerlerini POJO'ya çevirecek şöyle bir Converter
olsun. Burada aynı zamanda @ConfigurationPropertiesBinding kullanılıyor
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
@ConfigurationPropertiesBinding
@Component
public class FeatureToggleConverter implements Converter<String, FeatureToggle> {
@Override
public FeatureToggle convert(String isEnabled) {
return new FeatureToggle(Boolean.parseBoolean(isEnabled));
}
}
Daha sonra POJO taşıyıcısı sınıf şöyle
olsun. Burada @ConstructorBinding kullanılıyor.
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
@ConfigurationProperties(prefix = "feature.toggles")
@ConstructorBinding
public class FeatureToggles {
private final FeatureToggle showFormatted;
private final FeatureToggle debug;
public FeatureToggles(FeatureToggle showFormatted, FeatureToggle debug) {
this.showFormatted = showFormatted;
this.debug = debug;
}
public FeatureToggle showFormatted() {
return showFormatted;
}
public FeatureToggle debug() {
return debug;
}
}
Kullanmak için şöyle
yaparız@RestController
public class HelloController {
private final FeatureToggles featureToggles;
@Autowired
public HelloController(FeatureToggles featureToggles) {
this.featureToggles = featureToggles;
}
@GetMapping("/hello")
public String hello(@RequestParam String name) {
if (featureToggles.showFormatted().isEnabled()) {
return String.format("<h1>Hello, %s</h1>", name);
} else {
return String.format("Hello, %s", name);
}
}
}