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.
Spring Boot uses two primary types of property files for configuration:
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.
application.properties uses a simple key-value pair format (key=value), while application.yml uses YAML syntax.application.yml is generally more readable, especially for complex configurations with nested 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.
Create a file named application.properties in the src/main/resources directory and add the following content:
1server.port=80812app.name=MySpringBootApp3app.description=A simple Spring Boot application
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:
1import org.springframework.beans.factory.annotation.Value;2import org.springframework.stereotype.Component;34@Component5public class AppProperties {67@Value("${app.name}")8private String appName;910@Value("${app.description}")11private String appDescription;1213public void printAppInfo() {14System.out.println("Application Name: " + appName);15System.out.println("Application Description: " + appDescription);16}17}
Run your Spring Boot application and you should see the output:
$ ./mvnw spring-boot:run
Application Name: MySpringBootApp Application Description: A simple Spring Boot application
Now, let's configure the same properties using application.yml.
Create a file named application.yml in the src/main/resources directory and add the following content:
1server:2port: 80823app:4name: MySpringBootAppYaml5description: A simple Spring Boot application using YAML
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:
1import org.springframework.beans.factory.annotation.Value;2import org.springframework.stereotype.Component;34@Component5public class AppPropertiesYaml {67@Value("${app.name}")8private String appName;910@Value("${app.description}")11private String appDescription;1213public void printAppInfo() {14System.out.println("Application Name: " + appName);15System.out.println("Application Description: " + appDescription);16}17}
Run your Spring Boot application and you should see the output:
$ ./mvnw spring-boot:run
Application Name: MySpringBootAppYaml Application Description: A simple Spring Boot application using YAML
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.