BAEL-3745: Implement PropertySourceFactory to load YAML files (new module) (#9307)

This commit is contained in:
kwoyke 2020-05-17 12:59:16 +02:00 committed by GitHub
parent 44a1ec0195
commit 832b32210c
7 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,5 @@
## Spring Boot Properties
This module contains articles about Properties in Spring Boot.
### Relevant Articles:

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-properties-2</artifactId>
<name>spring-boot-properties-2</name>
<packaging>jar</packaging>
<description>Spring Boot Properties Module</description>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,22 @@
package com.baeldung.properties.yaml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class YamlApplication implements CommandLineRunner {
@Autowired
private YamlFooProperties yamlFooProperties;
public static void main(String[] args) {
SpringApplication.run(YamlApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("YAML Properties " + yamlFooProperties);
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.properties.yaml;
import com.baeldung.properties.yaml.factory.YamlPropertySourceFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.util.List;
@Configuration
@ConfigurationProperties(prefix = "yaml")
@PropertySource(value = "classpath:foo.yml", factory = YamlPropertySourceFactory.class)
public class YamlFooProperties {
private String name;
private List<String> aliases;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getAliases() {
return aliases;
}
public void setAliases(List<String> aliases) {
this.aliases = aliases;
}
@Override
public String toString() {
return "YamlFooProperties{" +
"name='" + name + '\'' +
", aliases=" + aliases +
'}';
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.properties.yaml.factory;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import java.io.IOException;
import java.util.Properties;
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(encodedResource.getResource());
Properties properties = factory.getObject();
return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties);
}
}

View File

@ -0,0 +1,5 @@
yaml:
name: foo
aliases:
- abc
- xyz

View File

@ -0,0 +1,23 @@
package com.baeldung.properties.yaml;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest
public class YamlFooPropertiesIntegrationTest {
@Autowired
private YamlFooProperties yamlFooProperties;
@Test
public void whenFactoryProvidedThenYamlPropertiesInjected() {
assertThat(yamlFooProperties.getName()).isEqualTo("foo");
assertThat(yamlFooProperties.getAliases()).containsExactly("abc", "xyz");
}
}