codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

Spring Boot

7 / 62 topics
6Spring Boot Auto-Configuration7Using application.properties and application.yml8Spring Profiles
Tutorials/Spring Boot/Using application.properties and application.yml
🍃Spring Boot

Using application.properties and application.yml

Updated 2026-05-15
10 min read

Using application.properties and application.yml

Introduction

Spring Boot simplifies the configuration of applications by providing a convention-over-configuration approach. One of the key features of Spring Boot is its ability to externalize your configuration so that you can work with the same application code in different environments. This tutorial will guide you through how to configure your Spring Boot application using application.properties and application.yml files.

Concept

Spring Boot uses two primary types of property files for configuration:

  1. application.properties: A simple key-value pair format.
  2. application.yml: A YAML-based format that is more readable and structured, especially useful for nested properties.

Both files are located in the src/main/resources directory by default. Spring Boot automatically loads these files during application startup and applies the configurations to your application context.

Key Differences

  • Format: application.properties uses a simple key-value pair format (key=value), while application.yml uses YAML syntax.
  • Readability: application.yml is generally more readable, especially for complex configurations with nested properties.
  • Usage: Both formats are equally supported by Spring Boot, and you can use either or both in your application.

Examples

Using application.properties

Let's start with a simple example using application.properties. Suppose you want to configure the server port and some custom properties for your application.

Step 1: Create the application.properties file

Create a file named application.properties in the src/main/resources directory and add the following content:

properties
1server.port=8081
2app.name=MySpringBootApp
3app.description=A simple Spring Boot application

Step 2: Accessing Properties

You can access these properties in your Spring Boot application using the @Value annotation or by injecting a Environment object.

Here's an example of accessing properties using the @Value annotation:

Java
1import org.springframework.beans.factory.annotation.Value;
2import org.springframework.stereotype.Component;
3
4@Component
5public class AppProperties {
6
7 @Value("${app.name}")
8 private String appName;
9
10 @Value("${app.description}")
11 private String appDescription;
12
13 public void printAppInfo() {
14 System.out.println("Application Name: " + appName);
15 System.out.println("Application Description: " + appDescription);
16 }
17}

Step 3: Run the Application

Run your Spring Boot application and you should see the output:

Terminal
$ ./mvnw spring-boot:run
Output
Application Name: MySpringBootApp
Application Description: A simple Spring Boot application

Using application.yml

Now, let's configure the same properties using application.yml.

Step 1: Create the application.yml file

Create a file named application.yml in the src/main/resources directory and add the following content:

YAML
1server:
2port: 8082
3app:
4name: MySpringBootAppYaml
5description: A simple Spring Boot application using YAML

Step 2: Accessing Properties

Accessing properties in application.yml is the same as with application.properties. You can use the @Value annotation or inject a Environment object.

Here's an example of accessing properties using the @Value annotation:

Java
1import org.springframework.beans.factory.annotation.Value;
2import org.springframework.stereotype.Component;
3
4@Component
5public class AppPropertiesYaml {
6
7 @Value("${app.name}")
8 private String appName;
9
10 @Value("${app.description}")
11 private String appDescription;
12
13 public void printAppInfo() {
14 System.out.println("Application Name: " + appName);
15 System.out.println("Application Description: " + appDescription);
16 }
17}

Step 3: Run the Application

Run your Spring Boot application and you should see the output:

Terminal
$ ./mvnw spring-boot:run
Output
Application Name: MySpringBootAppYaml
Application Description: A simple Spring Boot application using YAML

What's Next?

In the next section, we will explore how to use Spring Profiles to manage different configurations for various environments (e.g., development, testing, production).

By understanding and utilizing application.properties and application.yml, you can effectively manage your application's configuration and make it more flexible and maintainable.


PreviousSpring Boot Auto-ConfigurationNext Spring Profiles

Recommended Gear

Spring Boot Auto-ConfigurationSpring Profiles