1. Overview

This article is about spring boot yml properties example, we can manage spring boot configuration inapplication.properties or application.yml or application.yaml file.

  • application.properties or .yml file should be available inside /resources folder at root location. Here is a way to change the location of application.properties or .yml file.
  • Here is a priority to consider by spring boot if all three file available in /resources folder.
    • application.properties (1st property)
    • application.yml (2nd priority)
    • application.yaml (3rd priority)
  • Here are some advantages to use application.yml instead of application.properties file
    • Easy to read properties
    • Convenient syntax to hierarchical format
    • Let’s check here application.properties and application.yml so we can differentiate between reading and convinces:

application.properties

spring.application.name=spring boot yml example
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/test
server.port=9000

application.yml

spring:
   application:
     name: spring boot yml example
   datasource:
     driverClassName: com.mysql.jdbc.Driver
     url: jdbc:mysql://localhost/test
server:
   port: 9000

We have developed example maven, here is spring boot Gradle example using application.properties file, We can simply replace application.yml.

2. Example

spring boot yml properties example

spring boot yml properties example

2.1 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>spring-boot-example</groupId>
    <artifactId>spring-boot-yml-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <description>spring boot yml properties example</description>
    <packaging>jar</packaging>
    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.2 application.yml

server:
  port: 8989
  context-path: /demo
spring:
  application:
    name: spring yaml example

2.3 SpringBootConfig

package com.javadeveloperzone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
 * Created by JavaDeveloperZone on 06-05-2018.
 */
@SpringBootApplication
@ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootConfig.class, args);            // it wil start application
    }
}

2.4 HelloController

package com.javadeveloperzone.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * Created by JavaDeveloperZone on 06-05-2018.
 */
@RestController
public class HelloController {
    @RequestMapping(value = "/hello")
    public String hello() {
        return "Spring boot YML Example";
    }
}

2.5 Output

Let’s run maven project : mvn spring-boot:run

 .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
 ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/  ___)| |_)| | | | | || (_| |  ) ) ) )
 '  |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::        (v1.5.4.RELEASE)
2018-05-06 17:39:29.010  INFO 68888 --- [main] com.javadeveloperzone.SpringBootConfig   : Started SpringBootConfig in 6.506 seconds (JVM running for 42.699)

http://localhost:8989/demo/hello

spring boot yml properties example - output

spring boot yml properties example – output

3. References

4. Source Code

spring-boot-yaml-properties-example (173 KB)

Was this post helpful?

2 comments. Leave new

VEMA REDDY AMUDAM
June 16, 2018 8:22 am

Your tutorials are really helpful for beginners like me, i am going through your tutorial.
Another best thing is your reference documentation section providing link for the documentation to get more knowledge.
Thanks for the the tutorial, please help create simple tutorials for each concepts as many as possible.

Thank you vema.

You can go through our tutorial article where our all blogs links are available with proper category.

https://javadeveloperzone.com/spring-boot/spring-boot-tutorial/

Leave a Reply

Your email address will not be published. Required fields are marked *