[JAVA] Führen Sie die Verarbeitung manuell entsprechend @ConfigurationProperties durch

`` `@ ConfigurationProperties``` von Spring Boot ist ein Mechanismus, der automatisch den Einstellungswert der Eigenschaftendatei liest, aber ich möchte einen ähnlichen Prozess manuell beschreiben.

Angenommen, Sie haben jetzt eine `` kafka.properties``` wie die folgende und möchten diese in die `org.springframework.boot.autoconfigure.kafka.KafkaProperties``` laden.

kafka.properties


spring.kafka.bootstrap-servers=localhost:32770
spring.kafka.consumer.group-id=java-consumer-group

KafkaProperties


@ConfigurationProperties(prefix = "spring.kafka")
public class KafkaProperties {
  ...

Verwenden Sie dazu `MapConfigurationPropertySource``` mit Map als Quelle für die Einstellungen (`` Properties erbt von Hashtable```. Es gibt).

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;

public class HogeMain {
	public static void main(String[] args) throws IOException {
		Properties properties = new Properties();
		properties.load(Files.newInputStream(Paths.get("kafka.properties")));

		ConfigurationPropertySource source= new MapConfigurationPropertySource(properties);
		Binder binder = new Binder(source);

		KafkaProperties kafkaProperties = binder.bind("spring.kafka", KafkaProperties.class).get();
	}
}

Im Fall von Yaml können `` Eigenschaften``` von `YamlPropertiesFactoryBean``` bezogen werden, also lassen Sie es durch.

YamlPropertiesFactoryBean y = new YamlPropertiesFactoryBean();
y.setResources(new ClassPathResource("hoge.yml"));
Properties object = y.getObject();

Referenz

Recommended Posts

Führen Sie die Verarbeitung manuell entsprechend @ConfigurationProperties durch
Ich möchte die Aggregationsverarbeitung mit Spring-Batch durchführen
Einfach zu erstellende Verarbeitungsbibliothek
Ich möchte eine asynchrone Verarbeitung und periodische Ausführung mit Rail durchführen !!!