0
0
Spring Bootframework~30 mins

Profile-based configuration in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Profile-based configuration in Spring Boot
📖 Scenario: You are building a Spring Boot application that needs to behave differently in development and production environments. You want to use profile-based configuration to switch settings easily.
🎯 Goal: Create a Spring Boot application that uses application-dev.properties and application-prod.properties files to load different greeting messages based on the active profile.
📋 What You'll Learn
Create a base application.properties file
Create application-dev.properties with a greeting message
Create application-prod.properties with a different greeting message
Create a Spring Boot main class that reads the greeting message from properties
Use the @Value annotation to inject the greeting message
Use spring.profiles.active to switch between profiles
💡 Why This Matters
🌍 Real World
Profile-based configuration is used to manage different settings for development, testing, and production environments without changing code.
💼 Career
Understanding profile-based configuration is essential for backend developers working with Spring Boot to deploy applications safely and efficiently.
Progress0 / 4 steps
1
Create base application.properties and dev profile properties
Create a file named application.properties with no content. Then create application-dev.properties containing the line app.greeting=Hello from Development!.
Spring Boot
Need a hint?

The application.properties file can be empty. The application-dev.properties file must contain the exact line app.greeting=Hello from Development!.

2
Create prod profile properties with a different greeting
Create application-prod.properties containing the line app.greeting=Hello from Production!.
Spring Boot
Need a hint?

Make sure the application-prod.properties file contains the exact line app.greeting=Hello from Production!.

3
Create Spring Boot main class with greeting injection
Create a Spring Boot main class named ProfileDemoApplication in package com.example.demo. Use @Value("${app.greeting}") to inject the greeting into a String greeting field. Add a @RestController with a @GetMapping("/") method that returns the greeting string.
Spring Boot
Need a hint?

Use @Value("${app.greeting}") to inject the greeting. Create a nested @RestController class with a @GetMapping("/") method returning the greeting.

4
Set active profile and run the application
In application.properties, add the line spring.profiles.active=dev to activate the dev profile. This will make the application use the dev greeting message when running.
Spring Boot
Need a hint?

Add the exact line spring.profiles.active=dev to application.properties to activate the dev profile.