Introduction
Configuration helps your Spring Boot app work correctly in different places without changing code. It makes your app flexible and easier to manage.
Configuration helps your Spring Boot app work correctly in different places without changing code. It makes your app flexible and easier to manage.
springboot-app/src/main/resources/application.properties # Example configuration server.port=8080 spring.datasource.url=jdbc:mysql://localhost:3306/mydb featureX.enabled=true
application.properties or application.yml files to store configuration..properties or .yml files for configuration.# application.properties server.port=8081 # application.yml server: port: 8081
# Enable a feature
featureX.enabled=true# Different config for dev and prod # application-dev.properties server.port=8082 # application-prod.properties server.port=80
This Spring Boot app reads a configuration value featureX.enabled. It shows how configuration controls app behavior without changing code.
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } @RestController class HelloController { @Value("${featureX.enabled:false}") private boolean featureXEnabled; @GetMapping("/feature-status") public String featureStatus() { if (featureXEnabled) { return "Feature X is enabled"; } else { return "Feature X is disabled"; } } }
Always keep sensitive info like passwords out of public config files.
Use profiles to separate settings for different environments.
Configuration lets you change app behavior without code changes.
Spring Boot loads config files automatically at startup.
Use profiles to manage settings for different environments easily.
application.properties file?application.properties files.server.port=8080.application.properties content:spring.profiles.active=dev server.port=9090
dev profile?dev, and server.port=9090 is defined.server.port is set in the active profile, the app uses port 9090.application.properties and application-prod.properties. You want to run the app with the prod profile but the app still uses settings from application.properties. What is the likely cause?spring.profiles.active to select which profile config to load.spring.profiles.active is not set to prod, the app uses default application.properties.spring.profiles.active property is not set to prod. -> Option Ddev and prod environments in Spring Boot. Which approach correctly manages this using configuration?application-dev.properties and application-prod.properties with URLs and activating profiles is the standard way.application-dev.properties and application-prod.properties with respective URLs, then set spring.profiles.active accordingly. -> Option BPass the test and this pattern moves unknown