Spring boot external application.properties
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Spring Boot is a powerful framework used for building Java-based enterprise applications. It simplifies the process of setting up and deploying applications by providing a set of defaults and utilities out of the box. One significant feature of Spring Boot is its ability to externalize configuration via properties files, such as `application.properties` or `application.yml`. This article delves into the intricacies of using an external `application.properties` file in a Spring Boot application, providing insights and examples along the way.
Understanding External Configuration in Spring Boot
Spring Boot applications can be configured using properties files, YAML files, environment variables, and command-line arguments. Having an external `application.properties` file allows developers to change application settings without recompiling code, which greatly aids in maintaining consistency across development, testing, and production environments.
Default `application.properties` vs External Configuration
By default, Spring Boot looks for an `application.properties` file inside the `src/main/resources` directory. However, there are several ways to provide external configurations:
- Command-Line Arguments: Pass properties using `--` as in `java -jar app.jar --server.port=8081`.
- Environment Variables: Configure using environment variables like `export SERVER_PORT=8081`.
- External File: Use an external `application.properties` or `application.yml` file to customize settings beyond the default configuration.
Upon launch, Spring Boot follows a specific order in which these configurations are applied. External files take precedence over internal files if specified correctly.
Setting Up External Configuration
To use an external `application.properties` file, you need to specify the file path using the `spring.config.location` property when starting the Spring Boot application. For example:
- Profiles: Application properties can be organized into profiles. For example, `application-dev.properties` or `application-prod.properties`. Profile-specific properties are activated using the `spring.profiles.active` parameter.
- Multiple Config Locations: Specify multiple configuration files if needed:
- Separation of Concerns: Separating configuration from code allows for cleaner code and centralized configuration management.
- Environment-Specific Configurations: Easily manage different configurations for development, testing, and production.
- Dynamic Configuration: Update configurations without redeploying or rebuilding the application.
Related reading
- Spring boot fails to load DataSource using PostgreSQL driver
- Spring Boot fails to run maven-surefire-plugin ClassNotFoundException org.apache.maven.surefire.booter.ForkedBooter
- Spring Boot Getting Scheduled cron value from database
- Spring Boot gives TemplateInputException Error resolving template when running from jar
- Spring Boot Gradle how to build executable jar
- Spring Boot /h2-console throws 403 with Spring Security 1.5.2
- Spring Boot Handler dispatch failed; nested exception is java.lang.NoSuchMethodError
- Spring Boot Hibernate and Flyway boot order

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.