Spring Boot – YAML
YAML stands for “YAML Ain’t Markup Language“
YAML is built from the ground up to be simple to use. At its core, a YAML file is used to describe data. One of the benefits of using YAML is that the information in a single YAML file can be easily translated to multiple language types.
Click here to learn more about YAML.
Spring Boot YAML Configuration
The SpringApplication
class by default supports YAML as an alternative to properties whenever you have the SnakeYAML library on your classpath.
If you use “Starters”, SnakeYAML is automatically provided by
spring-boot-starter
.
- YAML files should end in
.yaml
or.yml
- YAML is case sensitive.
- YAML does not allow the use of tabs. Spaces are used instead as tabs are not universally supported.
Loading YAML Instead of Properties file
Spring Framework provides two convenient classes that can be used to load YAML documents. The YamlPropertiesFactoryBean
loads YAML as Properties
and the YamlMapFactoryBean
loads YAML as a Map
.
For example, consider the following YAML document:
app: name: TalksInfo url: https://talksinfo.com topics: - Maven - Spring Boot - Core Java - Design Patterns
The preceding example would be transformed into the following properties:
app.name=TalksInfo app.url=https://talksinfo.com app.topics[0]=Maven app.topics[1]=Spring Boot app.topics[2]=Core Java app.topics[3]=Design Patterns
YAML lists are represented as property keys with [index]
dereferencers. For example, consider the following YAML:
topics: - Maven - Spring Boot - Core Java - Design Patterns
Example Project using Spring Boot YAML
Create a new Spring Starter Project and project’s folder structure as follows,
POM.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.talksinfo</groupId> <artifactId>SpringYAML</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringYAML</name> <description>Demo project for Spring Properties file</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yaml
app: name: TalksInfo url: https://talksinfo.com topics: - Maven - Spring Boot - Core Java - Design Patterns
ApplicationProperties.java
@Configuration @EnableConfigurationProperties @ConfigurationProperties public class ApplicationProperties { private App app; public void setApp(App app) { this.app = app; } public static class App{ private String name; private String url; private List<String> topics; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public List<String> getTopics() { return topics; } public void setTopics(List<String> topics) { this.topics = topics; } } public App getApp() { return app; } }
The annotation used here are:
- @Configuration marks the class as a source of bean definitions
- @ConfigurationProperties binds and validates the external configurations to a configuration class
- @EnableConfigurationProperties this annotation is used to enable @ConfigurationProperties annotated beans in the Spring application
SpringYamlApplication.java
@SpringBootApplication public class SpringYamlApplication implements ApplicationRunner{ private Logger LOG = LoggerFactory.getLogger(SpringYamlApplication.class); @Autowired private ApplicationProperties appProperties; public static void main(String[] args) { SpringApplication.run(SpringYamlApplication.class, args); } @Override public void run(ApplicationArguments args) throws Exception { System.out.println(appProperties.getApp().getName()); System.out.println(appProperties.getApp().getUrl()); System.out.println(appProperties.getApp().getTopics()); } }
Console
TalksInfo https://talksinfo.com [Maven, Spring Boot, Core Java, Design Patterns]
When binding to Map
properties, if the key
contains anything other than lowercase alpha-numeric characters or -
, you need to use the bracket notation so that the original value is preserved. If the key is not surrounded by []
, any characters that are not alpha-numeric or -
are removed. For example, consider binding the following properties to a Map
:
app: map: "[/key1]": value1 "[/key2]": value2 /key3: value3
The properties above will bind to a Map
with /key1
, /key2
and key3
as the keys in the map.
Conclusion
In this quick article, we have seen how to use YAML in spring boot and how to replace application.properties file using YAML file. Also, usage of List, Arrays, Map in YAML is given an introduction.