How to Use Yamlpropertiesfactorybean to Load Yaml Files Using Spring Framework 4.1

How to use YamlPropertiesFactoryBean to load YAML files using Spring Framework 4.1?

With XML config I've been using this construct:

<context:annotation-config/>

<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources" value="classpath:test.yml"/>
</bean>

<context:property-placeholder properties-ref="yamlProperties"/>

Of course you have to have the snakeyaml dependency on your runtime classpath.

I prefer XML config over the java config, but I recon it shouldn't be hard to convert it.

edit:

java config for completeness sake

@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("default.yml"));
propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
return propertySourcesPlaceholderConfigurer;
}

Setting up Spring 4.1 profile environment using YAML instead of Properties and @Profile

I found this SPR where Chris Beams states why PropertySourcesPlaceholderConfigurer should not be registered automatically by Spring and why one should prefer user Environment over @value.

In addition, on Spring reference documentation, the Spring Testing Annotation section shows how to use ApplicationContextInitializer, which could be used to setup YamlPropertiesFactoryBean properly.

Spring Boot: Load @Value from YAML file

M. Deinum is right, the setup i've provided is working - the yml file was indented wrong, so the property couldn't be found.

Spring Boot - Load multiple YAML files

In the .yml file of the project add below content.

spring:
profiles:
include:
- currency

Note: You can refer to multiple .yml files this way if you want. You need additional .yml files and Config classes like below example.

You need to have another .yml file which is application-currency.yml

In application-currency.yml you can add currencies, like below

currencies:
mappings:
USD:
fraction: 2
symbol: $
text: "US Dollar"
MXN:
fraction: 2
symbol: $
text: "Mexican Peso"

Your Configuration class would look like below.

package com.configuration;

import com.Currency;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "currencies")
public class CurrencyConfiguration {

private Map<String, Currency> mappings;

public Map<String, Currency> getMappings() {
return mappings;
}

public void setMappings(Map<String, Currency> mappings) {
this.mappings = mappings;
}
}

Wherever you need to use the currency details you can get by calling as shown below.

@Autowired
private CurrencyConfiguration currencyConfiguration;

String currencyCodeAlpha3 = "USD";
Currency currency = currencyConfiguration.getMappings().get(currencyCodeAlpha3);

Spring Boot: how to use multiple yml files

@PropertySource does not support YAML (probably it will in Spring 4.1). You can set spring.config.location or spring.config.name to a comma-separated list (e.g. as System property or command line argument).

Personally I like all my YAML in the same place (the structure really helps to break it up visually, and you can use documents inside the file to split it up more). That's just taste I guess.



Related Topics



Leave a reply



Submit